青年人的教育是国家的基石。——富兰克林

题面:

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
/*

Intro:

Since we already have some of the additional
information about our users, it's a good idea
to output it in a nice way.

Exercise:

Fix type errors in logPerson function.

logPerson function should accept both User and Admin
and should output relevant information according to
the input: occupation for User and role for Admin.

*/

interface User {
name: string;
age: number;
occupation: string;
}

interface Admin {
name: string;
age: number;
role: string;
}

export type Person = User | Admin;

export const persons: Person[] = [
{
name: 'Max Mustermann',
age: 25,
occupation: 'Chimney sweep'
},
{
name: 'Jane Doe',
age: 32,
role: 'Administrator'
},
{
name: 'Kate Müller',
age: 23,
occupation: 'Astronaut'
},
{
name: 'Bruce Willis',
age: 64,
role: 'World saver'
}
];

export function logPerson(person: Person) {
let additionalInformation: string;
if (person.role) {
additionalInformation = person.role;
} else {
additionalInformation = person.occupation;
}
console.log(` - ${person.name}, ${person.age}, ${additionalInformation}`);
}

persons.forEach(logPerson);

// In case you are stuck:
// https://www.typescriptlang.org/docs/handbook/2/narrowing.html#the-in-operator-narrowing

输出Error

1
2
3
4
5
6
index.ts(58,16): error TS2339: Property 'role' does not exist on type 'Person'.
Property 'role' does not exist on type 'User'.
index.ts(59,40): error TS2339: Property 'role' does not exist on type 'Person'.
Property 'role' does not exist on type 'User'.
index.ts(61,40): error TS2339: Property 'occupation' does not exist on type 'Person'.
Property 'occupation' does not exist on type 'Admin'.

答案:

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
/*

Intro:

Since we already have some of the additional
information about our users, it's a good idea
to output it in a nice way.

Exercise:

Fix type errors in logPerson function.

logPerson function should accept both User and Admin
and should output relevant information according to
the input: occupation for User and role for Admin.

*/

interface User {
name: string;
age: number;
occupation: string;
}

interface Admin {
name: string;
age: number;
role: string;
}

export type Person = User | Admin;

export const persons: Person[] = [
{
name: 'Max Mustermann',
age: 25,
occupation: 'Chimney sweep'
},
{
name: 'Jane Doe',
age: 32,
role: 'Administrator'
},
{
name: 'Kate Müller',
age: 23,
occupation: 'Astronaut'
},
{
name: 'Bruce Willis',
age: 64,
role: 'World saver'
}
];

export function logPerson(person: Person) {
let additionalInformation: string;
if ("role" in person) {
additionalInformation = person.role;
} else {
additionalInformation = person.occupation;
}
console.log(` - ${person.name}, ${person.age}, ${additionalInformation}`);
}

persons.forEach(logPerson);

// In case you are stuck:
// https://www.typescriptlang.org/docs/handbook/2/narrowing.html#the-in-operator-narrowing

解析:

这里主要是用到了 in 的语法,注意细节如果是

1
if ("role" in person && person.role) {  }

会导致

1
2
index.ts(61,40): error TS2339: Property 'occupation' does not exist on type 'Person'.
Property 'occupation' does not exist on type 'Admin'.

所以只能使用

1
if ("role" in person) {  }