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

typescript OR (union)


  function numOrString(inp: string | number) {
    console.log(inp);
  }

  /*this means can be eiter a string or a number*/
Comment

PREVIOUS NEXT
Code Example
Typescript :: google sheets format number as duration formula 
Typescript :: how to use if statemnts c# 
Typescript :: ts code to move the next month 
Typescript :: custom link react 
Typescript :: execute script when c# code gets executed 
Typescript :: validation minlength angular 
Typescript :: watch ref.current changes typescript 
Typescript :: from how many ways we can define props with typescript react 
Typescript :: clean broken shortcuts in windows start menu 
Typescript :: react inherit html input props 
Typescript :: difference between scripted testing and exploratory testing 
Typescript :: typescript array 
Typescript :: persists meaning 
Typescript :: formgroup check if valid 
Typescript :: how to compile ts in cmd 
Typescript :: import ts in html 
Typescript :: python remove all double elements from list 
Typescript :: Interface with custom property name type 
Typescript :: read excel typescript 
Typescript :: What kind of projects is suitable for the Agile methodology 
Typescript :: typeorm configuration typescript 
Typescript :: handlebars custom helper 
Typescript :: share data across tab through localstorage 
Typescript :: testing in different environments 
Typescript :: get keys of an array angualr 
Typescript :: typescript compile stop using required 
Typescript :: ModuleNotFoundError brython 
Typescript :: phaser load progress 
Typescript :: how to use typescript map 
Typescript :: benefits of waxing body hair 
ADD CONTENT
Topic
Content
Source link
Name
7+2 =