type-challenges icon indicating copy to clipboard operation
type-challenges copied to clipboard

16 - Pop

Open zheeeng opened this issue 4 years ago • 29 comments

type Pop<T extends any[]> = T extends [...infer I, infer _] ? I : never

zheeeng avatar Aug 04 '20 10:08 zheeeng

Similarly,

type Pop<T extends any[]> = T extends [...infer P, any] ? P : never

FlorianWendelborn avatar Sep 04 '20 21:09 FlorianWendelborn

type Pop<T extends any[]> = T extends [...infer U, any] ? U : never
type Push<T extends any[], U> = [...T, U]
type Shift<T extends any[]> = T extends [any, ...infer U] ? U : never
type Unshift<T extends any[], U> = [U, ...T]

Jack-Works avatar Sep 05 '20 03:09 Jack-Works

Probably preferable to make the “any” become “unknown” in these examples since many default eslint configs as well as tsconfig like to complain about the use of any.

unknown works just as well here.

type Pop<T extends unknown[]> = T extends [...infer U, unknown] ? U : never
type Push<T extends unknown[], U> = [...T, U]
type Shift<T extends unknown[]> = T extends [unknown, ...infer U] ? U : never
type Unshift<T extends unknown[], U> = [U, ...T]

bradennapier avatar Sep 06 '20 19:09 bradennapier

type Pop<T extends any[]> = T extends [...infer I, infer _] ? I : never

RU: https://ru.stackoverflow.com/questions/1375991/%d0%9a%d0%b0%d0%ba-%d1%80%d0%b0%d0%b1%d0%be%d1%82%d0%b0%d0%b5%d1%82-%d1%82%d0%b8%d0%bf-pop/1376076#1376076

doox911-opensource avatar Jan 28 '22 08:01 doox911-opensource

I found that the last test case was not passing for me. I instead returned the array to comply with the last test case.

// my solution - notice, I return T if T does not extend an array that has items.
type Pop<T extends any[]> = T extends [...infer FirstSet, infer _] ? FirstSet : T

// last test case
Expect<Equal<Pop<[]>, []>>,

fredski02 avatar Apr 14 '23 13:04 fredski02

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

Same here this is what I landed on.

type Pop<T extends any[]> = T extends [...infer start, unknown] ? start : T;

? start : [] also works.

rickwillcox avatar Apr 25 '23 12:04 rickwillcox

This is what I came up with:

type Pop<T extends any[]> = T['length'] extends 0 ? [] : T extends [...infer First, infer Last] ? First : never;

amankr-novo avatar Apr 26 '23 11:04 amankr-novo

type Pop<T extends any[]> = T extends []
  ? []
  : T extends [...infer R, infer L]
  ? R
  : never;

stevenaces avatar May 06 '23 11:05 stevenaces

type Pop<T extends unknown[]> = T extends [...infer U, unknown] ? [...U] : T;

akasataikisiti avatar May 12 '23 09:05 akasataikisiti

type Pop<T extends any[]> = T extends [...infer P, any] ? P : T extends [] ? [] : never

HersanKuang avatar May 24 '23 07:05 HersanKuang

Why does the Pop type in the code doesn't assign Test a type of number[]? type Pop<T extends any[]> = T extends [... infer NT, unknown] ? NT : []; type Test = Pop<number[]>; // Test type = []. Why not number[]?

EltonLobo07 avatar May 27 '23 18:05 EltonLobo07

type Pop<T extends any[]> =T extends [...infer U,unknown]?U:[]

Liboq avatar Jul 18 '23 06:07 Liboq

The original answer fails this test case Expect<Equal<Pop<[]>, []>> type Pop<T extends any[]> = T extends [...infer I, unknown] ? I : []

Kying-star avatar Jul 18 '23 12:07 Kying-star

Can this use Exclude to solve? I write for following code, it is related to 15. Last of Array

type Last<T extends any[]> = T extends [...any, infer K] ? K : never;
type Pop<T extends any[]> = Exclude<T, Last<T>>;

But this doesn't work.

Whiskeyi avatar Jul 25 '23 09:07 Whiskeyi

type Pop<T extends unknown[]> = T extends [...infer item, any] ? [...item] : []

ydaydayup avatar Aug 29 '23 09:08 ydaydayup

For empty array [], it returns [].

case

Expect<Equal<Pop<[]>, []>>

answer

type Pop<T extends any[]> = T extends [...infer Rest, infer _] ? Rest : []

Seungwoo321 avatar Nov 23 '23 07:11 Seungwoo321

type Pop<T extends any[]> = T extends [...infer A, any] ? A : []

chenqy-yh avatar Dec 01 '23 10:12 chenqy-yh

Maybe I'm mssing something but all the above answer fail the test for Pop<[]> Need to return T if the extends is false:

type Pop<T extends unknown[]> = T extends [...infer Rest, infer _] ? Rest : T

timrutter avatar Dec 09 '23 08:12 timrutter

type Pop<arr extends any[]> = arr extends [...infer leftArr, any] ? leftArr : never

huanganfree avatar Dec 13 '23 08:12 huanganfree

type Pop<T extends any[]> = T extends [...infer U, any] ? U : never
type Push<T extends any[], U> = [...T, U]
type Shift<T extends any[]> = T extends [any, ...infer U] ? U : never
type Unshift<T extends any[], U> = [U, ...T]

I have a question that Shift method seems to return the first element in array instead return left array.So, it should be that type Shift<arr extends any[]> = arr extends [infer First, ...any[]] ? First : never . right? @antfu

huanganfree avatar Dec 13 '23 08:12 huanganfree

Expect<Equal<Pop<[]>, []>>, this won't pass but using type Pop<T extends any[]> = T["length"] extends 0 ? T : T extends [...infer TRest,infer _ ] ? TRest : never will make it pass

mssoheil avatar Dec 25 '23 05:12 mssoheil

latest version: type Pop<T extends any[]> = T extends [...infer A, any] ? A : []

Qzhor avatar Jan 03 '24 08:01 Qzhor

Maybe too much, but passed on all cases

type Pop<T extends any[], E extends any[] = []> = T extends [infer F, ...infer L] 
	? L extends [] // end of array
	? E // return accumulator
	: Pop<L, [...E, F]>  
	: E

danilowoz avatar Mar 12 '24 21:03 danilowoz

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

soeasyjx avatar Jun 03 '24 07:06 soeasyjx