人的一辈子都在高潮—低潮中浮沉,唯有庸碌的人,生活才如死水一般。——傅雷

问题:

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

Intro:

Our attempt to Open Source didn't work quite as
expected. It turned out there were already many
existing functional JS libraries.

All the remaining developers left the company as
well. It seems that they are joining a very
ambitious startup which re-invented a juicer and
raised millions of dollars.
Too bad we cannot compete with this kind of
financing even though we believe our idea is
great.

It's time to shine for the last time and publish
our new invention: object-constructor as our CTO
named it. A small library which helps
manipulating an object.

Exercise:

Here is a library which helps manipulating objects.
We tried to write type annotations and we failed.
Please help!

*/

export class ObjectManipulator {

constructor(protected obj) {}

public set(key, value) {
return new ObjectManipulator({...this.obj, [key]: value});
}

public get(key) {
return this.obj[key];
}

public delete(key) {
const newObj = {...this.obj};
delete newObj[key];
return new ObjectManipulator(newObj);
}

public getObject() {
return this.obj;
}
}

报错:

1
2
3
4
5
6
7
8
9
index.ts(32,17): error TS7006: Parameter 'obj' implicitly has an 'any' type.
index.ts(34,16): error TS7006: Parameter 'key' implicitly has an 'any' type.
index.ts(34,21): error TS7006: Parameter 'value' implicitly has an 'any' type.
index.ts(38,16): error TS7006: Parameter 'key' implicitly has an 'any' type.
index.ts(42,19): error TS7006: Parameter 'key' implicitly has an 'any' type.
test.ts(9,12): error TS2344: Type 'false' does not satisfy the constraint 'true'.
test.ts(19,12): error TS2344: Type 'false' does not satisfy the constraint 'true'.
test.ts(27,12): error TS2344: Type 'false' does not satisfy the constraint 'true'.
test.ts(33,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
/*

Intro:

Our attempt to Open Source didn't work quite as
expected. It turned out there were already many
existing functional JS libraries.

All the remaining developers left the company as
well. It seems that they are joining a very
ambitious startup which re-invented a juicer and
raised millions of dollars.
Too bad we cannot compete with this kind of
financing even though we believe our idea is
great.

It's time to shine for the last time and publish
our new invention: object-constructor as our CTO
named it. A small library which helps
manipulating an object.

Exercise:

Here is a library which helps manipulating objects.
We tried to write type annotations and we failed.
Please help!

*/

type ObjectWithNewProp<T, K extends string, V> = T & {[NK in K]: V};

export class ObjectManipulator<T> {
constructor(protected obj: T) {}

public set<K extends string, V>(key: K, value: V): ObjectManipulator<ObjectWithNewProp<T, K, V>> {
return new ObjectManipulator({...this.obj, [key]: value} as ObjectWithNewProp<T, K, V>);
}

public get<K extends keyof T>(key: K): T[K] {
return this.obj[key];
}

public delete<K extends keyof T>(key: K): ObjectManipulator<Omit<T, K>> {
const newObj = {...this.obj};
delete newObj[key];
return new ObjectManipulator(newObj);
}

public getObject(): T {
return this.obj;
}
}