-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathunion.ts
54 lines (43 loc) · 1017 Bytes
/
union.ts
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
42
43
44
45
46
47
48
49
50
51
52
53
54
let score: number | string;
score = 10;
score = "10";
type User4 = {
name: string;
age: number;
isLogin: boolean;
};
type Admin = {
name: string;
age: number;
isLogin: boolean;
admin: boolean;
};
let fahim: User4 | Admin;
fahim = {
name: "fahim",
age: 22,
isLogin: true,
admin: true,
};
function getId(id: number | string) {
return id;
}
getId(1);
getId("1");
function getId2(id: number | string) {
id.toLowerCase(); //err
}
function getId3(id: number | string) {
if (typeof id === "string") {
id.toLowerCase();
} else {
id.toExponential();
}
}
//array
const data: number[] = [1, 2, 3, 4, 5, 6, 7, 8, 9, 10];
const data1: Array<number> = [1, 2, 3, 4, 5, 6, 7, 8, 9, "10"]; //err
const data2: (number | string)[] = [1, 2, 3, 4, 5, 6, 7, 8, 9, "10"];
const data3: Array<number | string> = [1, 2, 3, 4, 5, 6, 7, 8, 9, "10"];
const data4: number[] | string[] = [1, 2, 3, 4, 5, 6, 7, 8, 9, 10];
const data5: number[] | string[] = [1, 2, 3, 4, 5, 6, 7, 8, 9, "10"]; //err