type Last<T extends any[]> = T extends [...infer Rest, infer Last] ? Last : never
Solution by eunsukimme #35122
type Last<T extends any[]> = T extends [...infer Rest, infer LastElement] ? LastElement : never
Solution by 2njeong #35081
// your answers
Solution by 2njeong #35080
type Last<T extends any[]> = T extends [...any[], infer L] ? L : never;
Solution by raeyoung-kim #35000
type Last<T extends any[]> = T extends [...any[], infer L] ? L : never;
Solution by 56aiden90 #34955
type Last<T extends any[]> = T extends [infer A, ...infer Rest]
? Rest extends [infer B, ...infer Rest2]
? Last
我这个方法可能不是最好的,但是如果是type Last<T extends any[]> = T extends [...any, infer L] ? L : T这样的,在实际运行代码层面又是不合理的,type Last<T extends any[]> = [any, ...T][T["length"]];又不好理解,所以我还是采用最原始的做法。
Solution by shx123qwe #34916
type Last<T extends unknown[]> = T extends [infer Last]
? Last
: T extends [infer _, ...infer Rest]
? Last<Rest>
: never;
Solution by SayaOvO #34823
// your answers
type Last<T extends any[]> = T extends [...any, infer L] ? L : never;
Solution by zeyuanHong0 #34796
type Last<T extends any[]> = T extends [...any[], infer M] ? M : never;
Solution by nathan2slime #34656
type Last<T extends unknown[]> = [never, ...T][T["length"]];
// or
type Last<T extends unknown[]> = T extends [... infer Head, infer Tail] ? Tail : never
Solution by binhdv155127 #34633
type Last<T extends any[]> = T['length'] extends 0 ? never : [any, ...T][T['length']]
// your answers
Solution by Rustamaha #34599
type Last<T extends any[]> = T['length'] extends 1
? T[0]
: T extends [infer I, ...infer R] ? Last<R> : never
// your answers
Solution by Rustamaha #34598
type Last<T extends any[]> = T extends [...infer rest, infer F] ? F : never
Solution by Hailey0930 #34569
type Last<T extends any[]> = T extends [...infer B,infer C] ? C : never
Solution by devshinthant #34565
// your answers
type Last<T extends any[]> = T extends [...infer F, infer L] ? L: never
Solution by gobielJonathan #34471
type Last<T extends any[]> = T extends [infer L, ...infer R] ? R['length'] extends 0 ? L : Last<R> : never
Solution by ktim816 #34435
type Last<T extends any[]> = T extends [infer A, ...infer Rest]
? Rest['length'] extends 0 ? A : Last<Rest>
: never
Solution by rookie-luochao #34386
// 你的答案
type Last<T extends any[]> = T extends [...infer A,infer B]?B:never
Solution by W-fitTiger #34255
이 챌린지에는 TypeScript 4.0 사용이 권장됩니다.
배열 T
를 사용하고 마지막 요소를 반환하는 제네릭 Last<T>
를 구현합니다.
예시
type arr1 = ["a", "b", "c"];
type arr2 = [3, 2, 1];
type tail1 = Last<arr1>; // expected to be 'c'
type tail2 = Last<arr2>; // expected to be 1
type Last<T extends any[]> = T[T["length"] - 1];
처음에는 다음과 같은 방식으로 접근했습니다. 하지만 타입스크립트에서는 타입시스템이 정수 "연산"을 지원하지 않기 떄문에, T["length"]-1을 접근이 안되는 문제가 있었습니다. 따라서 다른 방법이 필요했습니다.
type Last<T extends any[]> = T extends [...infer Rest, infer Last]
? Last
: never;
/* _____________ 여기에 코드 입력 _____________ */
type Last<T extends any[]> = T extends [...infer Rest, infer Last]
? Last
: never;
/* _____________ 테스트 케이스 _____________ */
import type { Equal, Expect } from "@type-challenges/utils";
type cases = [
Expect<Equal<Last<[2]>, 2>>,
Expect<Equal<Last<[3, 2, 1]>, 1>>,
Expect<Equal<Last<[() => 123, { a: string }]>, { a: string }>>
];
Solution by adultlee #34155
type Last<T extends any[]> = T extends [infer P, ...infer X]
? X extends []
? P
: Last<X>
: never;
Solution by maidao-yang #34123
type Last<T extends any[]> = T extends [...infer ANY, infer L] ? L : never;
Solution by G00syara #34040
type Last<T extends any[]> = T extends [...unknown[], infer last] ? last : never
Solution by ouzexi #33989
type Last<T extends any[]> = T extends [...infer _, infer Last] ? Last : never
Solution by notsecret32 #33891
type Last<T> = T extends [...infer P, infer R] ? R : never
Solution by Danny101201 #33832
type Last<T extends any[]> = T extends [...any, infer A] ? A : never
Solution by laplace1009 #33718
// 你的答案
type Last<T extends any[]> = T extends [...infer other, infer Last] ? Last : never
Solution by QHCLF #33680
type Last<T extends any[]> = T extends [...infer P, infer L] ? L : never;
Solution by nupthale #33581
type Last<T extends any[]> = T extends [infer F, ...infer Rest] ? Rest[number] extends never ? F : Last<Rest> : never
Solution by Flavour86 #33449
// 你的答案
type Last<T extends any[]> = T extends [...infer P, infer R] ? R : never
Solution by shaobeichen #33372
type Last<T extends any[]> = T extends [...args: any[], infer R] ? R : never;
Solution by chenghao125 #33296