00004-easy-pick

68 阅读1分钟

问题

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:

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

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

const todo: TodoPreview = {
    title: 'Clean room',
    completed: false,
}

解答

type MyPick<T extends Record<string, any>, U> = { [P in Extract<keyof Todo, U>]: T[P] }