Go Back

Refer: Easy - Unshift

Describe

Implement the type version of Array.unshift

For example

code snippetCopytypescript
type Result = Unshift<[1, 2], 0> // [0, 1, 2,]

Test Cases

code snippetCopytypescript
import { Equal, Expect, ExpectFalse, NotEqual } 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']>>, Expect<Equal<Unshift<['1', 2, '3'], '3'>, ['1', 2, '3']>>, ]

Solution

code snippetCopytypescript
type Unshift<T extends any[], U> = [U] extends [T[number]] ? T : [U, ...T]