typescript union types
type Cow = {
name: string;
moo: () => void;
};
type Dog = {
name: string;
bark: () => void;
};
type Cat = {
name: string;
meow: () => void;
};
// union type
type Animals = Cow | Dog | Cat;
typescript union types
type Cow = {
name: string;
moo: () => void;
};
type Dog = {
name: string;
bark: () => void;
};
type Cat = {
name: string;
meow: () => void;
};
// union type
type Animals = Cow | Dog | Cat;
union value typescript
let myVar : string | number; //Variable with union type declaration
myVar = 100; //OK
myVar = 'Lokesh'; //OK
myVar = true; //Error - boolean not allowed
union value typescript
let myVar : string | number; //myVar can store string and number types
use type as value typescript
Typescript interfaces aren''t being compiled into the js output, and you can not use them at runtime
typescript union
// Union Type: function reacts depending on x type (array of string OR string)
function welcomePeople(x: string[] | string) {
if (Array.isArray(x)) {
console.log("Hello, " + x.join(" and ")); // 'x' is 'string[]'
} else {
console.log("Welcome lone traveler " + x); // 'x' is 'string'
}
}
Copyright © 2021 Codeinu
Forgot your account's password or having trouble logging into your Account? Don't worry, we'll help you to get back your account. Enter your email address and we'll send you a recovery link to reset your password. If you are experiencing problems resetting your password contact us