-
Notifications
You must be signed in to change notification settings - Fork 0
/
00004-easy-pick.ts
73 lines (52 loc) · 1.31 KB
/
00004-easy-pick.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
/*
4 - Pick
-------
by Anthony Fu (@antfu) #easy #union #built-in
### Question
Implement the built-in `Pick<T, K>` generic without using it.
Constructs a type by picking the set of properties `K` from `T`
For example
```ts
interface Todo {
title: string
description: string
completed: boolean
}
type TodoPreview = MyPick<Todo, 'title' | 'completed'>
const todo: TodoPreview = {
title: 'Clean room',
completed: false,
}
```
> View on GitHub: https://tsch.js.org/4
*/
/* _____________ Your Code Here _____________ */
type MyPick<T, K extends keyof T> = {
[P in K]: T[P]
}
/* _____________ Test Cases _____________ */
import type { Equal, Expect } from '@type-challenges/utils'
type cases = [
Expect<Equal<Expected1, MyPick<Todo, 'title'>>>,
Expect<Equal<Expected2, MyPick<Todo, 'title' | 'completed'>>>,
// @ts-expect-error
MyPick<Todo, 'title' | 'completed' | 'invalid'>,
]
interface Todo {
title: string
description: string
completed: boolean
}
interface Expected1 {
title: string
}
interface Expected2 {
title: string
completed: boolean
}
/* _____________ Further Steps _____________ */
/*
> Share your solutions: https://tsch.js.org/4/answer
> View solutions: https://tsch.js.org/4/solutions
> More Challenges: https://tsch.js.org
*/