Skip to content
Onsite

MyPick<T, K>

EasyP1Coding

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

MyPick<T, K> builds a type by picking the set of properties K from T. K must be limited to keys of T, so MyPick<Todo, 'missing'> is a type error.

Tests your type must pass

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

interface Expected1 {
  title: string
}

interface Expected2 {
  title: string
  completed: boolean
}

type cases = [
  Expect<Equal<Expected1, MyPick<Todo, 'title'>>>,
  Expect<Equal<Expected2, MyPick<Todo, 'title' | 'completed'>>>,
  // @ts-expect-error 'invalid' is not a key of Todo
  MyPick<Todo, 'title' | 'completed' | 'invalid'>,
]

Hints

TypeScript
Run and Submit type-check your solution against the tests.