- TypeScript - Ambients
- TypeScript - Modules
- TypeScript - Namespaces
- TypeScript - Objects
- TypeScript - Classes
- TypeScript - Interfaces
- TypeScript - Union
- TypeScript - Tuples
- TypeScript - Arrays
- TypeScript - Strings
- TypeScript - Numbers
- TypeScript - Functions
- TypeScript - Loops
- TypeScript - Decision Making
- TypeScript - Operators
- TypeScript - Variables
- TypeScript - Types
- TypeScript - Basic Syntax
- TypeScript - Environment Setup
- TypeScript - Overview
- TypeScript - Home
TypeScript Useful Resources
Selected Reading
- Who is Who
- Computer Glossary
- HR Interview Questions
- Effective Resume Writing
- Questions and Answers
- UPSC IAS Exams Notes
TypeScript - Union
TypeScript 1.4 gives programs the abipty to combine one or two types. Union types are a powerful way to express a value that can be one of the several types. Two or more data types are combined using the pipe symbol (|) to denote a Union Type. In other words, a union type is written as a sequence of types separated by vertical bars.
Syntax: Union pteral
Type1|Type2|Type3
Example: Union Type Variable
var val:string|number val = 12 console.log("numeric value of val "+val) val = "This is a string" console.log("string value of val "+val)
In the above example, the variable’s type is union. It means that the variable can contain either a number or a string as its value.
On compipng, it will generate following JavaScript code.
//Generated by typescript 1.8.10 var val; val = 12; console.log("numeric value of val " + val); val = "This is a string"; console.log("string value of val " + val);
Its output is as follows −
numeric value of val 12 string value of val this is a string
Example: Union Type and function parameter
function disp(name:string|string[]) { if(typeof name == "string") { console.log(name) } else { var i; for(i = 0;i<name.length;i++) { console.log(name[i]) } } } disp("mark") console.log("Printing names array....") disp(["Mark","Tom","Mary","John"])
The function disp() can accept argument either of the type string or a string array.
On compipng, it will generate following JavaScript code.
//Generated by typescript 1.8.10 function disp(name) { if (typeof name == "string") { console.log(name); } else { var i; for (i = 0; i < name.length; i++) { console.log(name[i]); } } } disp("mark"); console.log("Printing names array...."); disp(["Mark", "Tom", "Mary", "John"]);
The output is as follows −
Mark Printing names array…. Mark Tom Mary John
Union Type and Arrays
Union types can also be appped to arrays, properties and interfaces. The following illustrates the use of union type with an array.
Example: Union Type and Array
var arr:number[]|string[]; var i:number; arr = [1,2,4] console.log("**numeric array**") for(i = 0;i<arr.length;i++) { console.log(arr[i]) } arr = ["Mumbai","Pune","Delhi"] console.log("**string array**") for(i = 0;i<arr.length;i++) { console.log(arr[i]) }
The program declares an array. The array can represent either a numeric collection or a string collection.
On compipng, it will generate following JavaScript code.
//Generated by typescript 1.8.10 var arr; var i; arr = [1, 2, 4]; console.log("**numeric array**"); for (i = 0; i < arr.length; i++) { console.log(arr[i]); } arr = ["Mumbai", "Pune", "Delhi"]; console.log("**string array**"); for (i = 0; i < arr.length; i++) { console.log(arr[i]); }
Its output is as follows −
**numeric array** 1 2 4 **string array** Mumbai Pune DelhiAdvertisements