-
Notifications
You must be signed in to change notification settings - Fork 0
/
3、类型守卫.ts
74 lines (60 loc) · 1.57 KB
/
3、类型守卫.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
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
/*
类型保护是可执行运行时检查的一种或表达式,用于确保该类型在一定范围内
*/
// 1、 in 关键字
interface Admin {
name: string,
privileges: string[]
}
interface Employee {
name: string,
startDate: Date;
}
type UnknownEmployee = Employee | Admin;
function printEmployeeInformation(emp: UnknownEmployee) {
console.log('Name:' + emp.name);
if ('privileges' in emp) {
console.log('Privileges:' + emp.privileges)
}
if ("startDate" in emp) {
console.log("strtDate:" + emp.startDate)
}
}
// 2、typeof 关键字
function padLeft(val: string, padding: string | number) {
if (typeof padding === 'number') {
return Array(padding + 1).join(" ") + val
}
if (typeof padding === 'string') {
return padding + val
}
throw new Error(`Expected string or number, got '${padding}'.`)
}
// 3、instanceof 关键字
interface Padder {
getPaddingString(): string;
}
class SpaceRepeatingPadder implements Padder {
constructor(private numSpace: number) { }
getPaddingString() {
return Array(this.numSpace + 1).join(" ");
}
}
class StringPadder implements Padder {
constructor(private value: string) { }
getPaddingString() {
return this.value
}
}
let padder: Padder = new SpaceRepeatingPadder(6);
if (padder instanceof SpaceRepeatingPadder) {
// padder 的类型收窄为 'SpaceRepeatingPadder'
}
// 4、自定义类型保护的类型谓词
function isNumber(x: any): x is number {
return typeof x === 'number';
}
function isString(x: any): x is string {
return typeof x === 'string';
}
console.log(isNumber('12'))