|
|
TypeScript 1.4 makes use of unions to combine one or two types. It can be done using a pipe symbol (|) denote a Union Type.
Example: Union Type Variable
In the example above, the variable’s type is union. It means that the variable can consists of either a number or a string as its value.
Upon compiling, it will produce the following JavaScript code.
Its result is as below −
Example: Union Type and function parameter
We can see a function disp() which accepts either of the type string or a string array.
Upon compiling, it will produce the following JavaScript code.
The result is as below −
Union types are applicable to arrays, properties and interfaces. The below example illustrates the use of union type with an array.
Example: Union Type and Array
In above program, an array is declared which represents either a numeric collection or a string collection.
Upon compiling, it will produce the below JavaScript code.
Its result is as follows −
|
|
All rights reserved © 2020 Wisdom IT Services India Pvt. Ltd
Wisdomjobs.com is one of the best job search sites in India.