-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathdemo-composition.ts
More file actions
42 lines (28 loc) · 823 Bytes
/
demo-composition.ts
File metadata and controls
42 lines (28 loc) · 823 Bytes
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
//-----> Composition types
// Unions
let myUnionVariable: string | number = "str";
myUnionVariable = 10;
// myUnionVariable = false; // error
// Literals
let myStringLiteral: "on" | "off" = "off";
let myNumberLiteral: 10 = 10;
let myBooleanLiteral: true = true;
myStringLiteral = 'on';
// myStringLiteral = 'suspend'; // error
// Intersection types
const myIntersectionVariable: {str: string} & {num: number} = {
str : "str",
num: 10
};
// Arrays
const myStringArrayVariable: any[] = ['str', '10'];
const myUnionArrayVariable: (string | number)[] = ["str", 10];
// Tuples
const myTupleVariable: [number, string] = [10, "str"];
// const myTupleVariable2: [string, number] = [10, "str"]; // error
// Enums
enum Color {Red, Green, Blue};
Color.Red // 0
Color.Blue // 2
Color[1] // "Green"
Color[2] // "Blue"