00014-easy-first

Back

// 你的答案
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

type First<T extends unknown[]> = T extends [infer R, ...unknown[]] ? R : never

Solution by souzaramon #34317

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

Solution by binhdv155127 #34313

注意,当为空数组时,返回类型为never会比为undefined更好。 结合之前的提交结果再补充几种写法:

// 写法一:采用extends []判断
type First<T extends any[]> = T extends [] ? never : T[0]

//写法二:通过T['length']获取到数组长度进行判断
type First<T extends any[]> = T['length'] extends 0 ? never : T[0]

//写法三:使用infer关键字
type First<T> = T extends [infer P, ...infer Rest] ? P : never

Solution by wxh-cyber #34292

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

Solution by wxh-cyber #34280

// 你的答案

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

Solution by W-fitTiger #34244

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

Solution by bkdragon0228 #34241

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

Solution by Fmanuel809 #34189

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

Solution by quitone #34182

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

Solution by ProvorovOleksii #34076

// 1차 --> infer 활용 할 생각 못 함. infer 활용하는 것을 보고 짜보기. --> 테스트코드 실패.
type First<T extends any[]> = T extends [infer F, ...any[]] ? F : null;

//2차  --> null이 아닌 never 넣어줘야 한다!! 조건이 맞지 않는 경우를 명확하게 하기 위함
type First<T extends any[]> = T extends [infer F, ...any[]] ? F : never;

Solution by leeeunji93 #34033