00014-easy-first

Back

type First<T extends any[]> = T extends [infer A,...infer U] ? A:never

Solution by TaueFenCheng #35533

type First<T extends any[]> = T extends [infer F, ...any[]] ? F : never;

Solution by thukyaw11 #35527

// ์—ฌ๊ธฐ ํ’€์ด๋ฅผ ์ž…๋ ฅํ•˜์„ธ์š”
type First<T extends any[]> = T extends [] ? never : T[0]

Solution by Gwanghun-Im #35498

type First<T extends any[]> = T['length'] extends 0 ? never : T[0];

Solution by gangnamssal #35468

type TupleToObject<T extends readonly PropertyKey[]> = {
  [P in T[number]] : P
}

Solution by RanungPark #35431

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

Solution by RanungPark #35430

// your answers
type First<T extends any[]> = T[0];

Solution by Sathiyapramod #35419

// your answers
type arr1 = ["1", "2", "3"];

type First<T extends any[]> = T[0];

type head1 = First<arr1>;

Solution by Sathiyapramod #35411

type FirstElement<T extends any[]> = T extends [] ? never : T[0]

// ไฝ ็š„็ญ”ๆกˆ

Solution by dixiaioping #35386

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

Solution by gyeounjeong #35352

// ไฝ ็š„็ญ”ๆกˆ
type First<T extends any[]> = T[0] extends undefined ? undefined : T[0];
// JS ็š„ๆ€่ทฏ๏ผŒ [][0] === undefined 

type First<T extends any[]> = T extends [infer F, ...any[]] ? F : undefined;
// TS ็š„ๆ€่ทฏ๏ผŒ็ฑปๅž‹ๆŽจๅฏผ็ฌฌไธ€ไธชๅ€ผ็š„็ฑปๅž‹
// ๅœจ่ฟ™ไธคไธช็ญ”ๆกˆไธญ๏ผŒๆˆ‘้ƒฝ่ฟ”ๅ›žไบ† undefined ่€Œไธๆ˜ฏ never๏ผŒ่ฎฉๅฎƒๆ›ดๅƒ JS ็š„็œŸๅฎž่ฟ่กŒๆƒ…ๅ†ต

Solution by ren-142326 #35254

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

Solution by Muntazir-sd #35221

type First<T extends any[]> = T extends [infer F, ...any[]] ? F : never;

Solution by raeyoung-kim #34927

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

Solution by Kim-Ji-Seop #34923

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

Solution by jsk3342 #34903

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

Solution by 56aiden90 #34875

๋ฌธ์ œ

๋ฐฐ์—ด(ํŠœํ”Œ) T๋ฅผ ๋ฐ›์•„ ์ฒซ ์›์†Œ์˜ ํƒ€์ž…์„ ๋ฐ˜ํ™˜ํ•˜๋Š” ์ œ๋„ค๋ฆญ First<T>๋ฅผ ๊ตฌํ˜„ํ•˜์„ธ์š”.

infer ํ‚ค์›Œ๋“œ ?

์กฐ๊ฑด๋ถ€ extends์ ˆ์—์„œ ์‚ฌ์šฉํ•˜๋Š” ํ‚ค์›Œ๋“œ์ด๋‹ค.

ํƒ€์ž… ์Šคํฌ๋ฆฝํŠธ๊ฐ€ ์—”์ง„์ด ๋Ÿฐํƒ€์ž„ ์ƒํ™ฉ์—์„œ ํƒ€์ž…์„ ์ถ”๋ก ํ•  ์ˆ˜ ์žˆ๋„๋ก ํ•˜๊ณ , ์ถ”๋ก ํ•œ ํƒ€์ž… ๊ฐ’์„ infer ํƒ€์ž… ํŒŒ๋ผ๋ฏธํ„ฐ U ์— ํ• ๋‹นํ•ด์ค€๋‹ค.

type Foo<T> = T extends { a: infer U; b: infer U } ? U : never;
type T10 = Foo<{ a: string; b: string }>; // string
type T11 = Foo<{ a: string; b: number }>; // string | number

ํ’€์ด

์กฐ๊ฑด๋ถ€ extends์ ˆ๊ณผ ์‚ผํ•ญ์—ฐ์‚ฐ์ž๋ฅผ ํ™œ์šฉํ•˜์˜€๊ณ , ๋ฐฐ์—ด์˜ ์Šคํ”„๋ ˆ๋“œ ์—ฐ์‚ฐ๊ณผ infer ํ‚ค์›Œ๋“œ๋ฅผ ํ†ตํ•ด ์กฐ๊ฑด๋ถ€ ํƒ€์ž… ์ถ”๋ก ์œผ๋กœ ๋ฌธ์ œ๋ฅผ ํ•ด๊ฒฐํ•˜์˜€๋‹ค.

// ์—ฌ๊ธฐ ํ’€์ด๋ฅผ ์ž…๋ ฅํ•˜์„ธ์š”
type First<T extends unknown[]> = T extends [infer A, ...infer rest]
  ? A
  : never;

Solution by MyeonghoonNam #34822

// your answers
type First<T extends any[]> = T extends [] ? never : T[0] extends any ? T[0] : never

Solution by eunsukimme #34809

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

Solution by kang-kibong #34743

type First<T extends any[]> = T extends []? never: T[0];

Solution by lephuthuc2001 #34697

// your answers
type First<T extends any[]> = T extends [infer First, ...infer Rest] ? First : never;

Solution by zeyuanHong0 #34672

type First<T extends any[]> =  T extends [infer F, ...any[]] ? F : never

Solution by nathan2slime #34655

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

Solution by devshinthant #34541

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

Solution by JayoonKoo #34519

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

Solution by Hailey0930 #34489

type First<T extends any[]> = T extends [] ? never : T[0];

Solution by ktim816 #34420

// your answers
type First<T extends any[]> = T extends [] ? never:  T[0]

Solution by gobielJonathan #34394

type First<T extends any[]> = T extends [] ? never : T[0];
type First<T extends any[]> = T['length'] extends 0 ? never : T[0];

Solution by znar #34384

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

Solution by rookie-luochao #34357

ๆœฌ้ข˜่งฃ็ญ”๏ผš

export type First<T extends any[]> = T extends [infer first, ...any[]]? first: never;

ไปฅไธ‹ๆ˜ฏๅฏนไธๅŒ็š„ๆ•ฐ็ป„ๆ“ไฝœ็š„TSๅฎž็Žฐ๏ผš

//ๆ•ฐ็ป„ๆœ€ๅŽไธ€ไธชๅ…ƒ็ด 
export type Last<T extends any[]>=T extends [...any,infer L]?L:never;

//ๅผนๅ‡บๆ•ฐ็ป„ๆœ€ๅŽไธ€ไธชๅ…ƒ็ด ๏ผˆPop๏ผ‰
export type Pop<T extends any[]>=T extends [...infer P,infer R]?P:never;

//ๅ‘ๆ•ฐ็ป„ๆœซๅฐพๆทปๅŠ ไธ€ไธชๅ…ƒ็ด ๏ผˆPush๏ผ‰
export type Push<T extends any[],U>=T extends [..T,U];

//ๅผนๅ‡บๆ•ฐ็ป„ๅผ€ๅคด็š„ๅ…ƒ็ด ๏ผˆShift๏ผ‰
export type Shift<T extends any[]>=T extends [infer F,...infer R]?R:never;

//ๅฐ†ๆ•ฐ็ป„ๆœ€ๅŽไธ€ไธชๅ…ƒ็ด ็ฝฎไบŽๆ•ฐ็ป„ๅผ€ๅคด๏ผˆUnshift๏ผ‰
export type Unshift<T extends any[],U>=[U,...T];

//่ฟžๆŽฅไธคไธชๆ•ฐ็ป„๏ผˆConcat๏ผ‰
export type Concat<T extends any[],V extends any[]>=[...T,...V];

//ๆ•ฐ็ป„ๅ่ฝฌ๏ผˆReverse๏ผ‰
export type Reverse<T extends any[]>=T extends [infer Head,...infer Rest]?[...Reverse<Rest>,Head]:T;

//่ฟ‡ๆปคๆŒ‡ๅฎšๅฏน่ฑก๏ผˆFilterOut๏ผ‰
export type FilterOut<T extends any[],F>=T extends [infer First,...infer Rest]?(First extends F?FilterOut<Rest,F>:[First,...FilterOut<Rest,F>]):T;

่ฏดๆ˜Ž๏ผšๆ•ฐ็ป„ๅ่ฝฌๅ’Œ่ฟ‡ๆปคๆŒ‡ๅฎšๅฏน่ฑก็š„ๅฎž็Žฐ้ƒฝ้‡‡็”จไบ†้€’ๅฝ’็š„ๆ–นๅผ๏ผŒไปฅ่พพๅˆฐๅฏนๆ•ฐ็ป„ไธญๆฏไธชๅ…ƒ็ด ่ฟ›่กŒๆŒ‡ๅฎš็š„ๆ“ไฝœใ€‚

Solution by wxh-cyber #34322