Answers for "typescript union"

3

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;
Posted by: Guest on December-03-2020
2

union value typescript

let myVar : string | number;        //Variable with union type declaration
 
myVar = 100;            //OK
myVar = 'Lokesh';       //OK
 
myVar = true;           //Error - boolean not allowed
Posted by: Guest on April-14-2021
2

union value typescript

let myVar : string | number;    //myVar can store string and number types
Posted by: Guest on April-14-2021
1

use type as value typescript

Typescript interfaces aren''t being compiled into the js output, and you can not use them at runtime
Posted by: Guest on November-16-2020
2

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'
  }
}
Posted by: Guest on April-24-2021

Code answers related to "TypeScript"

Browse Popular Code Answers by Language