-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy path1.basic_types.ts
More file actions
113 lines (87 loc) · 2.28 KB
/
1.basic_types.ts
File metadata and controls
113 lines (87 loc) · 2.28 KB
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
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
// 1. Boolean Type
let isDone: boolean = false;
log(isDone);
// 2. Number Type, all is floating point values
let decimal: number = 6;
log(decimal);
let hex: number = 0xf00d;
log(hex);
let binary: number = 0b1010;
log(binary);
let octal: number = 0o744;
log(octal);
// 3. String type typescript also use " and ', but for code format we suggest to use '
let color: string = "blue";
color = "red";
let fullName: string = `Bob Bobbington`;
let age: number = 37;
let sentence: string = `Hello, my name is ${fullName}. I'll be ${age +
1} years old next month.`;
log(sentence);
// 4. Array
let list: number[] = [1, 2, 3, 4];
let listSecondWay: Array<number> = [1, 2, 3];
// 5. Tuple
let x: [string, number];
x = ["hello", 10];
// x = [10, "hello"]; // Error
console.log(x[0].substring(1));
// console.log(x[1].substring(1)); //Error
// x[3] = "world";
// console.log(x[5].toString());
//6. Enum, default start with number 0
enum Color {
Red = 1,
Green = 2,
Blue = 0xf00d // To do, 枚举可以采用位运算
}
let colorName: string = Color[2];
log(colorName);
log(Color.Red);
function log(logInfo: any) {
console.log(`${JSON.stringify(logInfo)}`);
}
//7. Any
let notSure: any = 4;
notSure = "maybe a string instead";
notSure = false;
let notSureTwo: any = 4;
// notSure.ifItExists();
// notSure.toFixed();
let prettySure: Object = 4;
// prettySure.toFixed();
let listThree: any[] = [1, true, "free"];
listThree[1] = 100;
log(listThree);
// 8. void
function warnUser(): void {
console.log("This is my warning message");
}
// void only assign null and undefined to them
let unusable: void = undefined;
//9. Null and Undefined
let u: undefined = undefined;
let n: null = null;
// 9. Never
// Function returning never must have unreachable end point
function error(message: string): never {
throw new Error(message);
}
function fail() {
return error("Something failed");
}
function infiniteLoop(): never {
while (true) {}
}
//10. Object
declare function create(o: object | null): void;
create({ prop: 0 });
create(null);
create(42); //Error
create("string"); // Error
create(false); //Error
create(undefined); // Error
let someValue = "this is a string";
let strLength: number = (<string>someValue).length;
let someValue2 = "this is a string";
let strLength: number = (someValue as string).length;