你要克服的是你的虚荣心、炫耀欲,你要对付的是你时刻想要出风头的小聪明。——毛姆

题目:

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

Welcome to:

................................................................
. .
. #################### #################### E .
. #################### #################### X .
. #### #### E .
. #### #### R .
. #### #################### C .
. #### #### I .
. #### #### S .
. #### #################### E .
. #### #################### S .
. .
................................................................

The goal: Let everyone play with many different TypeScript features
and get an overview of TypeScript capabilities and principles.

Things to cover:

1. Basic typing.
2. Refining types.
3. Union types.
4. Merged types.
5. Generics.
6. Type declarations.
7. Module augmentation.
8. Advanced type mapping.

Rules and principles:

1. Avoid using "any" type at all costs.
2. Difficulty quickly grows one exercise after another.
3. Feel free to send pull requests if you've come up
with improvements!
4. Provide feedback to the creator of these exercises.
5. Enjoy.

Brief UI guide:

+--------------------------------------------------------------+
| TypeScript exercises |
+--------------------------------------------------------------+
| Exercises 1·2·3·4... << Navigate through exercises >> |
+---------------+----------------------------------------------+
| Files | file.ts << Filename and status >> |
+---------------+----------------------------------------------+
| file.ts | 1 import {x} from 'y'; |
| dir | 2 |
| sub.ts | 3 |
| | |
| << Current | << Currently selected file code editor >> |
| exercise file | |
| structure >> +----------------------------------------------+
| | |
| | << Errors to fix in order to proceed >> |
| | |
+---------------+----------------------------------------------+

Intro:

We are starting a small community of users. For performance
reasons, we have decided to store all users right in the code.
This way we can provide our developers with more
user-interaction opportunities. With user-related data, at least.
All the GDPR-related issues will be solved some other day.
This would be the basis for our future experiments during
these exercises.

Exercise:

Given the data, define the interface "User" and use it accordingly.

*/

export type User = unknown;

export const users: unknown[] = [
{
name: 'Max Mustermann',
age: 25,
occupation: 'Chimney sweep'
},
{
name: 'Kate Müller',
age: 23,
occupation: 'Astronaut'
}
];

export function logPerson(user: unknown) {
console.log(` - ${user.name}, ${user.age}`);
}

console.log('Users:');
users.forEach(logPerson);


// In case you are stuck:
// https://www.typescriptlang.org/docs/handbook/2/objects.html

输出Error

1
2
3
4
5
index.ts(95,23): error TS2571: Object is of type 'unknown'.
index.ts(95,37): error TS2571: Object is of type 'unknown'.
test.ts(4,12): error TS2344: Type 'false' does not satisfy the constraint 'true'.
test.ts(5,12): error TS2344: Type 'false' does not satisfy the constraint 'true'.
test.ts(6,12): error TS2344: Type 'false' does not satisfy the constraint 'true'.

答案:

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

Welcome to:

................................................................
. .
. #################### #################### E .
. #################### #################### X .
. #### #### E .
. #### #### R .
. #### #################### C .
. #### #### I .
. #### #### S .
. #### #################### E .
. #### #################### S .
. .
................................................................

The goal: Let everyone play with many different TypeScript features
and get an overview of TypeScript capabilities and principles.

Things to cover:

1. Basic typing.
2. Refining types.
3. Union types.
4. Merged types.
5. Generics.
6. Type declarations.
7. Module augmentation.
8. Advanced type mapping.

Rules and principles:

1. Avoid using "any" type at all costs.
2. Difficulty quickly grows one exercise after another.
3. Feel free to send pull requests if you've come up
with improvements!
4. Provide feedback to the creator of these exercises.
5. Enjoy.

Brief UI guide:

+--------------------------------------------------------------+
| TypeScript exercises |
+--------------------------------------------------------------+
| Exercises 1·2·3·4... << Navigate through exercises >> |
+---------------+----------------------------------------------+
| Files | file.ts << Filename and status >> |
+---------------+----------------------------------------------+
| file.ts | 1 import {x} from 'y'; |
| dir | 2 |
| sub.ts | 3 |
| | |
| << Current | << Currently selected file code editor >> |
| exercise file | |
| structure >> +----------------------------------------------+
| | |
| | << Errors to fix in order to proceed >> |
| | |
+---------------+----------------------------------------------+

Intro:

We are starting a small community of users. For performance
reasons, we have decided to store all users right in the code.
This way we can provide our developers with more
user-interaction opportunities. With user-related data, at least.
All the GDPR-related issues will be solved some other day.
This would be the basis for our future experiments during
these exercises.

Exercise:

Given the data, define the interface "User" and use it accordingly.

*/

export type User = {
name:string
age:number
occupation:string
};

export const users: User[] = [
{
name: 'Max Mustermann',
age: 25,
occupation: 'Chimney sweep'
},
{
name: 'Kate Müller',
age: 23,
occupation: 'Astronaut'
}
];

export function logPerson(user: User) {
console.log(` - ${user.name}, ${user.age}`);
}

console.log('Users:');
users.forEach(logPerson);


// In case you are stuck:
// https://www.typescriptlang.org/docs/handbook/2/objects.html