type Last<T extends any[]> = T extends [...infer A,infer U] ? U : never
Solution by TaueFenCheng #35534
type Last<T extends unknown[]> = T extends [...arg: unknown[], v:infer V] ? V : never
Solution by RanungPark #35481
// your answers
type arrOne = ["a", "b", "c"];
type arrTwo = [3, 2, 1];
type Last<T extends unknown[]> = T extends [...infer Pending, infer L] ? L : never;
const tail1: Last<arrOne> = "c";
const tail2: Last<arrTwo> = 1;
Solution by Sathiyapramod #35452
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