Go Back
Refer: Easy - Unshift
Describe
Implement the type version of Array.unshift
For example
code snippetCopytypescripttype Result = Unshift<[1, 2], 0> // [0, 1, 2,]
Test Cases
code snippetCopytypescriptimport { 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 snippetCopytypescripttype Unshift<T extends any[], U> = [U] extends [T[number]] ? T : [U, ...T]