TypeScript Union

Union in typescript are used to define the data type of variable when variable store more than one data type values.

let data: (string | number | boolean);

data = 10; //ok
console.log("data : ",data);

data = true; //ok
console.log("data : ",data);

data = "Code"; //ok
console.log("data : ",data);
data :  10
data :  true
data :  Code

Union as function parameter

We can use union to define variable of function parameter list.

function message(data:(string|number)){
  console.log("data : ",data);
message("Hello Programmer");
Union as function parameter
data :  Hello Programmer
data :  999


Please share your knowledge to improve code and content standard. Also submit your doubts, and test case. We improve by your feedback. We will try to resolve your query as soon as possible.

New Comment

© 2022, kalkicode.com, All rights reserved