-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy path00527-medium-append-to-object.ts
54 lines (45 loc) · 1.07 KB
/
00527-medium-append-to-object.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
// ============= Test Cases =============
import type { Equal, Expect } from './test-utils'
type test1 = {
key: 'cat'
value: 'green'
}
type testExpect1 = {
key: 'cat'
value: 'green'
home: boolean
}
type test2 = {
key: 'dog' | undefined
value: 'white'
sun: true
}
type testExpect2 = {
key: 'dog' | undefined
value: 'white'
sun: true
home: 1
}
type test3 = {
key: 'cow'
value: 'yellow'
sun: false
}
type testExpect3 = {
key: 'cow'
value: 'yellow'
sun: false
isMotherRussia: false | undefined
}
type cases = [
Expect<Equal<AppendToObject<test1, 'home', boolean>, testExpect1>>,
Expect<Equal<AppendToObject<test2, 'home', 1>, testExpect2>>,
Expect<Equal<AppendToObject<test3, 'isMotherRussia', false | undefined>, testExpect3>>,
]
// ============= Your Code Here =============
type AppendToObject<T, U extends string, V> = {
[K in (keyof T) | U]: K extends keyof T ? T[K]: V
}
// wrong answer:
// fix ver: https://github.com/type-challenges/type-challenges/issues/13265
// type AppendToObject<T, U extends string, V> = T & Record<U, V>