Search
 
SCRIPT & CODE EXAMPLE
 

TYPESCRIPT

union types typescript

/*
TypeScript allows us to use more than one data type for a variable 
or a function parameter. This is called union type.
*/
let code: (string | number);
code = 123;   // OK
code = "ABC"; // OK
code = false; // Compiler Error

let empId: string | number;
empId = 111; // OK
empId = "E111"; // OK
empId = true; // Compiler Error
Comment

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;
  
Comment

union value typescript

let myVar : string | number;        //Variable with union type declaration
 
myVar = 100;            //OK
myVar = 'Lokesh';       //OK
 
myVar = true;           //Error - boolean not allowed
Comment

union value typescript

let myVar : string | number;    //myVar can store string and number types
Comment

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'
  }
}
Comment

PREVIOUS NEXT
Code Example
Typescript :: how to create empty object typescript 
Typescript :: how to react typescript callback function¨ 
Typescript :: react typescript cheat sheet 
Typescript :: typescript generic dictionary 
Typescript :: redux persist typescript 
Typescript :: react-excel-renderer nextjs error 
Typescript :: property decorator typescript constructor 
Typescript :: typescript final example 
Typescript :: search an array of objects with specific object property value 
Typescript :: factory design pattern typescript 
Typescript :: cannot find file does not match the corresponding name on disk 
Typescript :: typescript keyof typeof 
Typescript :: typescript object type 
Typescript :: filter() array of objects on change react 
Typescript :: web.contents timeout 
Typescript :: how to remove the last item from a collection powerapps 
Typescript :: sails.ps1 cannot be loaded because running scripts is disabled on this system. For more information, see about_Execution_Policies 
Typescript :: update behaviorsubject value without emitting 
Typescript :: TypeError: key must be an instance of a class implements jwt.AbstractJWKBase 
Typescript :: Error in plugin @nomiclabs/hardhat-etherscan: The constructor for contracts/DAVID.sol:GuiltyDavid has 4 parameters but 0 arguments were provided instead. 
Typescript :: laravel no tests executed 
Typescript :: promise.all inside useEffect 
Typescript :: bits required for address 1 GB memory 
Typescript :: typescript array of empty objects 
Typescript :: typescript assert non null 
Typescript :: typescript dynamic interface 
Typescript :: generator typescript 
Typescript :: ts Facade pattern 
Typescript :: serenity-is change button text 
Typescript :: Checking if multiple elements are rendering using jasmine 
ADD CONTENT
Topic
Content
Source link
Name
5+8 =