Language/Typescript
타입챌린지 : 18-Length of Tuple (easy)
hsloth
2023. 3. 2. 17:21
이 글은 제가 타입챌린지를 하면서 해석한 내용을 적는 글입니다. 틀린 내용이 있으면 댓글 달아주시면 감사하겠습니다.
GitHub - type-challenges/type-challenges: Collection of TypeScript type challenges with online judge
Collection of TypeScript type challenges with online judge - GitHub - type-challenges/type-challenges: Collection of TypeScript type challenges with online judge
github.com
type Length<T extends readonly any[]> = T['length'];
이건 간단하다.
as const로 타입화 된 배열의 원소는 삭제, 수정할 수 없기 때문에 readonly any[] 를 상속받은 T를 정의할 수 있고, 배열에는 length 속성이 무조건 있기 때문에 T['length']를 리턴해주면 끝!