16 - Pop (exta: Shift, Push, Unshift)

Author: ganda1feroCreated Sep 8, 2026Updated Sep 8, 2026
Labelsansweren16
type Pop<T extends any[]> = T extends [...args: infer ArrT, _: any]
  ? ArrT
  : [];

type Shift<T extends any[]> = T extends [_: any, ...args: infer ArrT]
  ? ArrT
  : [];

type Push<T extends any[], V> = [...T, V];

type Unshift<T extends any[], V> = [V, ...T];

tests

import type { Equal, Expect } from '@type-challenges/utils'

type cases = [
  Expect<Equal<Pop<[3, 2, 1]>, [3, 2]>>,
  Expect<Equal<Pop<['a', 'b', 'c', 'd']>, ['a', 'b', 'c']>>,
  Expect<Equal<Pop<[]>, []>>,

  Expect<Equal<Shift<[3, 2, 1]>, [2, 1]>>,
  Expect<Equal<Shift<['a', 'b', 'c', 'd']>, ['b', 'c', 'd']>>,
  Expect<Equal<Shift<[]>, []>>,

  Expect<Equal<Push<[3, 2, 1], 0>, [3, 2, 1, 0]>>,
  Expect<Equal<Push<['a', 'b', 'c', 'd'], 'e'>, ['a', 'b', 'c', 'd', 'e']>>,
  Expect<Equal<Push<[], 1>, [1]>>,

  Expect<Equal<Unshift<[3, 2, 1], 4>, [4, 3, 2, 1]>>,
  Expect<Equal<Unshift<['a', 'b', 'c', 'd'], 'hi'>, ['hi', 'a', 'b', 'c', 'd']>>,
  Expect<Equal<Unshift<[], 1>, [1]>>,
];

Source: type-challenges/type-challenges