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

 

๋ฐ˜์‘ํ˜•

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