03060-easy-unshift

Back

Why the boolean one is complaining ?

type Unshift<T extends any[], U> = U extends [infer First, ...infer Rest] 
  ? [First, ...Rest, ...T] 
  : [U, ...T]

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

type cases = [
  Expect<Equal<Unshift<[], [1]>, [1]>>,
  Expect<Equal<Unshift<[], 1>, [1]>>,
  Expect<Equal<Unshift<[1, 2], 0>, [0, 1, 2]>>,
  Expect<Equal<Unshift<['1', 2, '3'], boolean>, [boolean, '1', 2, '3']>>,
]

Solution by julien-sibille #37524

type Unshift<T extends unknown[], U> = [U, ...T]

Breaking it down:

In plain words:

Unshift<T, U> constructs a new tuple type by:

  1. Placing the single element U at the beginning.
  2. Adding all elements from tuple T after it.
  3. Producing a new tuple type that represents the array after an unshift operation.

✅ Examples:

type A = [1, 2, 3]
type B = ["world", "!"]

type Test1 = Unshift<A, 0> // [0, 1, 2, 3] ✅ type Test2 = Unshift<B, "hello"> // ["hello", "world", "!"] ✅ type Test3 = Unshift<[], "first"> // ["first"] ✅ type Test4 = Unshift<[2, 3], [1]> // [[1], 2, 3] ✅ (unshifts the whole array as one element)

Solution by 3aluw #37493

type Unshift<T extends unknown[], U> = [U,...T];

Solution by BangTtagGum #37305

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

Solution by Nakamura25257 #37263

type Unshift<T extends unknown[], U> = [U, ...T];

Solution by djdidi #37138

Same answer as Push challenge

type Unshift<T extends unknown[], U> = [U,...T]

Solution by Anonymous961 #37063

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

Solution by 359Steve #37021

// your answers
type Unshift<T extends readonly any[], U> = [U, ...T];

Solution by AlexBraunMagic #36911

type Unshift<T extends unknown[], U> = [U, ...T];

Solution by shaishab316 #36849

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

/* _____________ Test Cases _____________ */
import type { Equal, Expect } from '@type-challenges/utils'

type cases = [
  Expect<Equal<Unshift<[], 1>, [1]>>,
  Expect<Equal<Unshift<[1, 2], 0>, [0, 1, 2]>>,
  Expect<Equal<Unshift<['1', 2, '3'], boolean>, [boolean, '1', 2, '3']>>,
]

Solution by AnastasiaSv #36769

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

Solution by tungulin #36720

// 여기 풀이를 입력하세요
type Unshift<T extends readonly any[], U> = [U, ...T]

Solution by seungdeok #36683

// your answers

type Unshift<T, U> = T extends [...infer TType] ? [U , ...TType] : never;

Suggestions are welcomed 🎉

Solution by SubramaniyanTN #36676

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

Solution by tungulin #36609

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

Solution by ChemieAi #36557

type Unshift<T extends Array<unknown>, U> = [U, ...T]

Solution by Divcutu #36467

type Unshift<T extends readonly any[], U> = [U , ...T]

Solution by alirezaprime #36418

type Unshift<T extends readonly  unknown[], U> = [U ,...T]

Solution by HelloWook #36402

// your answers
type Unshift<T extends readonly any[], U> = [U, ...T];

Solution by justBadProgrammer #36393

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

Solution by tac-tac-go #36362

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

Solution by 1Alex4949031 #36317

type Includes<T extends readonly any[], U> =
  T extends [infer First, ...infer Rest]
    ? Equal<First, U> extends true
      ? true
      : Includes<Rest, U>
    : false

Solution by 1Alex4949031 #36314

type Unshift<T extends unknown[], U> = [U, ...T]

Solution by asylbekduldiev #36296

type Unshift<T extends unknown[], U> = [U, ...T]

Solution by reonce #36051

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

Solution by karazyabko #36033

type Unshift<T extends unknown[], U> = [U, ...T];

Solution by codingaring #35943

// 你的答案
type Unshift<T extends readonly unknown[], U extends unknown> = [U, ...T]

Solution by naruto-823 #35796

type Unshift<T extends unknown[], U> = [U, ...T]

Solution by ydkim120 #35733

type Unshift<T extends Array<unknown> | ReadonlyArray<unknown>, U> = [U, ...T]

Solution by gangnamssal #35494

type Unshift<T extends unknown [], U> = [U, ...T]

Solution by RanungPark #35444