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