
/*
11 - Tuple to Object
-------
by sinoon (@sinoon) #쉬움 #object-keys
### 질문
배열(튜플)을 받아, 각 원소의 값을 key/value로 갖는 오브젝트 타입을 반환하는 타입을 구현하세요.
예시:
const tuple = ['tesla', 'model 3', 'model X', 'model Y'] as const
type result = TupleToObject<typeof tuple> // expected { tesla: 'tesla', 'model 3': 'model 3', 'model X': 'model X', 'model Y': 'model Y'}
> GitHub에서 보기: https://tsch.js.org/11/ko
*/
/* _____________ 여기에 코드 입력 _____________ */
// 문제 예시
// type TupleToObject<T extends readonly any[]> = any
type TupleToObject<T extends readonly PropertyKey[]> = {
[K in T[number]]: K
}
/* _____________ 테스트 케이스 _____________ */
import type { Equal, Expect } from '@type-challenges/utils'
const tuple = ['tesla', 'model 3', 'model X', 'model Y'] as const
const tupleNumber = [1, 2, 3, 4] as const
const tupleMix = [1, '2', 3, '4'] as const
type cases = [
Expect<Equal<TupleToObject<typeof tuple>, { tesla: 'tesla'; 'model 3': 'model 3'; 'model X': 'model X'; 'model Y': 'model Y' }>>,
Expect<Equal<TupleToObject<typeof tupleNumber>, { 1: 1; 2: 2; 3: 3; 4: 4 }>>,
Expect<Equal<TupleToObject<typeof tupleMix>, { 1: 1; '2': '2'; 3: 3; '4': '4' }>>,
]
// @ts-expect-error
type error = TupleToObject<[[1, 2], {}]>
/* _____________ 다음 단계 _____________ */
/*
> 정답 공유하기: https://tsch.js.org/11/answer/ko
> 정답 보기: https://tsch.js.org/11/solutions
> 다른 문제들: https://tsch.js.org/ko
*/
시작전에 생각했던 힌트
tuple 에서 무언가 빼올 수 있는 키워드가 있을까type TupleToObject<T extends readonly any[]> = {
[K in T[keyof T]] : K
}
keyof T 를 실행하니 'string | number | symbol' 타입으로 지정되어 T[number]로 정리type TupleToObject<T extends readonly any[]> = {
[K in T[number]] : K
}
@ts-expect-error 부분에서 에러를 띄워주는 이유를 생각해보다가 결국 정답을 보니any[] 가 아닌 PropertyKey[]로 제네릭 자리에 넣어주었다type TupleToObject<T extends readonly PropertyKey[]> = {
[K in T[number]] : K
}
⭐️ Easy라고 되어있지만 꽤나 Easy하지 않은 문제.. PropertyKey를 포함해서 한번 더 타입 구현에 대해서 되돌아볼 필요가 있다.
출처