๐Ÿ’™ 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

 

๋ฐ˜์‘ํ˜•

'๐Ÿ’™ Typescript > type-challenges' ์นดํ…Œ๊ณ ๋ฆฌ์˜ ๋‹ค๋ฅธ ๊ธ€

[TS] type-challenges: 189 - Awaited  (0) 2023.07.17
[TS] type-challenges: 43. Exclude  (0) 2023.07.14
[TS] type-challenges: 14. First of Array  (0) 2023.07.04
[TS] type-challenges : 11. Tuple to Object  (0) 2023.07.03
[TS] type-challenges : 7. Readonly  (0) 2023.06.28