๐ Typescript/type-challenges
[TS] type-challenges: 14. First of Array
์ ๋ฌ
2023. 7. 4. 20:52
๋ฐ์ํ
๋ฌธ์
๋ฐฐ์ด(ํํ) 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
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
๋ฐ์ํ