StudyForYou / ouahhan-typescript-with-react

์šฐ์•„ํ•œ ํƒ€์ž…์Šคํฌ๋ฆฝํŠธ with ๋ฆฌ์•กํŠธ ์Šคํ„ฐ๋”” ๋ ˆํฌ ๐Ÿงต
4 stars 0 forks source link

๐Ÿ”ฅtype-challenges_05_ 00014 - First of Array #45

Closed hyeyoonS closed 2 months ago

hyeyoonS commented 2 months ago

First of Array ์‰ฌ์›€ #array

by Anthony Fu @antfu

๋„์ „ํ•˜๊ธฐ    English ็ฎ€ไฝ“ไธญๆ–‡ ๆ—ฅๆœฌ่ชž

๋ฐฐ์—ด(ํŠœํ”Œ) 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

๋Œ์•„๊ฐ€๊ธฐ ์ •๋‹ต ๊ณต์œ ํ•˜๊ธฐ ์ •๋‹ต ๋ณด๊ธฐ

๊ด€๋ จ๋œ ๋ฌธ์ œ๋“ค

15ใƒปLast of Array
hyeyoonS commented 2 months ago
type First<T extends unknown[]> = T extends [] ? never : T[0]
summerkimm commented 2 months ago
type First<T extends any[]> = T extends [] ? never : T[0]
drizzle96 commented 2 months ago

type First<T extends any[]> = T extends [unknown, ...any] ? T[0] : never

hyeyoonS commented 2 months ago
type First<T extends any[]> = T extends [infer First, ...any] ? First : never
type First<T extends unknown[]> = T extends [infer First, ...any] ? First : never
summerkimm commented 2 months ago
type First<T extends any[]> = T["length"] extends 0 ? never : T[0]