๐Ÿ’™ Typescript/type-challenges

[TS] type-challenges: 18. Length of Tuple

์„ ๋‹ฌ 2023. 7. 5. 15:31
๋ฐ˜์‘ํ˜•

๋ฌธ์ œ

๋ฐฐ์—ด(ํŠœํ”Œ)์„ ๋ฐ›์•„ ๊ธธ์ด๋ฅผ ๋ฐ˜ํ™˜ํ•˜๋Š” ์ œ๋„ค๋ฆญ Length<T>๋ฅผ ๊ตฌํ˜„ํ•˜์„ธ์š”.

 

type tesla = ['tesla', 'model 3', 'model X', 'model Y']
type spaceX = ['FALCON 9', 'FALCON HEAVY', 'DRAGON', 'STARSHIP', 'HUMAN SPACEFLIGHT']

type teslaLength = Length<tesla>  // expected 4
type spaceXLength = Length<spaceX> // expected 5

 

ํ’€์ด

type Length<T extends any[]> = T['length'];

๊ฐ„๋‹จํ•˜๊ฒŒ ์ด๋ ‡๊ฒŒ ํ‘œํ˜„ํ–ˆ๋”๋‹ˆ

 

Type 'readonly ["tesla", "model 3", "model X", "model Y"]' does not satisfy the constraint 'any[]'.

์ด๋Ÿฐ๊ฒŒ ๋–ด๋‹ค

 

type Length<T extends readonly any[]> = T['length'];

readonly๋ฅผ ์ œ๋„ค๋ฆญ์— ๋ถ™์—ฌ์คฌ๋”๋‹ˆ ์„ฑ๊ณต

 

์ถœ์ฒ˜

https://github.com/type-challenges/type-challenges/blob/main/questions/00018-easy-tuple-length/README.ko.md

 

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

 

๋ฐ˜์‘ํ˜•