# Easy - Pick

# question

Implement the built-in Pick<T, K> generic without using it.

不要使用内置的 Pick<T, K>,自己实现一个。

Constructs a type by picking the set of properties K from T.

从类型 T 中选择出属性 K,构造成一个新的类型。

For example:

interface Todo {
  title: string
  description: string
  completed: boolean
}

type TodoPreview = MyPick<Todo, 'title' | 'completed'>

const todo: TodoPreview = {
    title: 'Clean room',
    completed: false,
}
1
2
3
4
5
6
7
8
9
10
11
12

# answer

type MyPick<T,K extends keyof T> = {
  [P in K]: T[K]
};
1
2
3
上次更新: 8/17/2022, 4:51:05 PM