typescript array from type union code example
Example 1: typescript union
function welcomePeople(x: string[] | string) {
if (Array.isArray(x)) {
console.log("Hello, " + x.join(" and "));
} else {
console.log("Welcome lone traveler " + x);
}
}
Example 2: union value typescript
let myVar : string | number;
myVar = 100;
myVar = 'Lokesh';
myVar = true;
Example 3: typescript union types
type Cow = {
name: string;
moo: () => void;
};
type Dog = {
name: string;
bark: () => void;
};
type Cat = {
name: string;
meow: () => void;
};
type Animals = Cow | Dog | Cat;