Search
 
SCRIPT & CODE EXAMPLE
 
CODE EXAMPLE FOR TYPESCRIPT

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'
  }
}
Source by www.typescriptlang.org #
 
PREVIOUS NEXT
Tagged: #typescript #union
ADD COMMENT
Topic
Name
5+9 =