Pick 타입을 구현하라

/*
  4 - Pick<T, K>
  -------
  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 `T` from `K`
  
  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> = any

/* _____________ Test Cases _____________ */ import { Equal, Expect } from '@type-challenges/utils'

type cases = [ Expect<Equal<Expected1, MyPick<Todo, 'title'>>>, Expect<Equal<Expected2, MyPick<Todo, 'title' | 'completed'>>>, ]

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 */


처음엔 다음과 같이 시도 했었다.

```tsx
type MyPick<T, K> = {
  [P in K]: T[P]
}

그러나 다음과 같은 메시지를 발견했고

K의 타입을 symbol로 좁혀 보았다.

type MyPick<T, K extends symbol> = {
  [P in K]: T[P]
}

그러나 여전히 오류가 발생했고 P는 T의 인덱스가 될수 없다고 하였다.

사실 지금 생각해 보면 간단하고 당연한데 해결이 잘 되지 않았다. 생각해보니 K의 타입을 더 상세하게 좁히면 되는 간단한 문제 였다.

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

끝!