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

  1. interface Todo {
  2. title: string
  3. description: string
  4. completed: boolean
  5. }
  6. type TodoPreview = MyPick<Todo, 'title' | 'completed'>
  7. const todo: TodoPreview = {
  8. title: 'Clean room',
  9. completed: false,
  10. }

Answer

  1. type MyPick<T, K extends keyof T> = {
  2. [P in K]: T[P]
  3. }