๐ 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๋ฅผ ์ ๋ค๋ฆญ์ ๋ถ์ฌ์คฌ๋๋ ์ฑ๊ณต
์ถ์ฒ
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
๋ฐ์ํ