A union type literal is written as a sequence of types separated by vertical bars. UnionType: PrimaryOrUnionType | PrimaryType A union typle literal references a union type. When function or constructor types are included in union types they must be enclosed in parentheses
29 lines
905 B
TypeScript
29 lines
905 B
TypeScript
//// [unionTypeLiterals.ts]
|
|
// basic valid forms of union literals
|
|
|
|
var simpleUnion: string | number;
|
|
var unionOfUnion: string | number | boolean;
|
|
|
|
var arrayOfUnions: (string | number)[];
|
|
var arrayOfUnions: Array<string | number>;
|
|
|
|
var unionOfFunctionType: (() => string) | (() => number);
|
|
var unionOfFunctionType: { (): string } | { (): number };
|
|
var unionOfFunctionType: () => string | number;
|
|
|
|
var unionOfConstructorType: (new () => string) | (new () => number);
|
|
var unionOfConstructorType: { new (): string } | { new (): number };
|
|
var unionOfConstructorType: new () => string | number;
|
|
|
|
//// [unionTypeLiterals.js]
|
|
// basic valid forms of union literals
|
|
var simpleUnion;
|
|
var unionOfUnion;
|
|
var arrayOfUnions;
|
|
var arrayOfUnions;
|
|
var unionOfFunctionType;
|
|
var unionOfFunctionType;
|
|
var unionOfFunctionType;
|
|
var unionOfConstructorType;
|
|
var unionOfConstructorType;
|
|
var unionOfConstructorType;
|