00015-medium-last

Back

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 : A : never;

ๆˆ‘่ฟ™ไธชๆ–นๆณ•ๅฏ่ƒฝไธๆ˜ฏๆœ€ๅฅฝ็š„๏ผŒไฝ†ๆ˜ฏๅฆ‚ๆžœๆ˜ฏ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;

solution

/* _____________ ์—ฌ๊ธฐ์— ์ฝ”๋“œ ์ž…๋ ฅ _____________ */

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