주어진 튜플을 키와 값이 동일한 객체 타입으로 만들어야 한다.
/*
11 - Tuple to Object
-------
by sinoon (@sinoon) #easy
### Question
Given an array, transform to a object type and the key/value must in the given array.
For example
```ts
const tuple = ['tesla', 'model 3', 'model X', 'model Y'] as const
const result: TupleToObject<tuple> // expected { tesla: 'tesla', 'model 3': 'model 3', 'model X': 'model X', 'model Y': 'model Y'}
View on GitHub: https://tsch.js.org/11 */
/* _____________ Your Code Here _____________ */
type TupleToObject<T extends readonly any[]> = any
/* _____________ Test Cases _____________ */ import { Equal, Expect } from '@type-challenges/utils'
const tuple = ['tesla', 'model 3', 'model X', 'model Y'] as const
type cases = [ Expect<Equal<TupleToObject<typeof tuple>, { tesla: 'tesla'; 'model 3': 'model 3'; 'model X': 'model X'; 'model Y': 'model Y'}>>, ]
/* _____________ Further Steps _____________ / /
Share your solutions: https://tsch.js.org/11/answer View solutions: https://tsch.js.org/11/solutions More Challenges: https://tsch.js.org */
역시나 어렵게 생각했나 보다....
```tsx
type TupleToObject<T extends readonly any[]> = {
[K in T[number]]: K
}
T[number]
형태로 접근 하고 in
으로 반복 하면서 K
로 값을 뽑아낸다.