๋ฐ์ํ
๋ฌธ์
๋ฐฐ์ด(ํํ) T๋ฅผ ๋ฐ์ ์ฒซ ์์์ ํ์ ์ ๋ฐํํ๋ ์ ๋ค๋ฆญ First<T>๋ฅผ ๊ตฌํํ์ธ์.
type arr1 = ['a', 'b', 'c']
type arr2 = [3, 2, 1]
type head1 = First<arr1> // expected to be 'a'
type head2 = First<arr2> // expected to be 3
ํ์ด
๋น๊ต์ ๊ฐ๋จํ ๋ฌธ์ ๋ผ์ T[0]์ ๋ฐํํ๋๋ก ํ๋๋ฐ, ๊ทธ๋ฌ๋๋ []์ธ๊ฒฝ์ฐ never์ ๋ํ ์ผ์ด์ค์ ๊ฑธ๋ ค์ ๋ถ๊ธฐ์ฒ๋ฆฌ๋ฅผ ์งํํด์คฌ๋ค
type First<T extends any[]> = T extends [] ? never : T[0];
์ถ์ฒ
https://github.com/type-challenges/type-challenges/blob/main/questions/00014-easy-first/README.ko.md
๋ฐ์ํ
'๐ Typescript > type-challenges' ์นดํ ๊ณ ๋ฆฌ์ ๋ค๋ฅธ ๊ธ
[TS] type-challenges: 43. Exclude (0) | 2023.07.14 |
---|---|
[TS] type-challenges: 18. Length of Tuple (0) | 2023.07.05 |
[TS] type-challenges : 11. Tuple to Object (0) | 2023.07.03 |
[TS] type-challenges : 7. Readonly (0) | 2023.06.28 |
[TS] type-challenges : 4. Pick (0) | 2023.06.28 |