I'm taking type-challenge exercise with the easy part, implement the type version of Unshift.(https://github.com/type-challenges/type-challenges/tree/master/questions/3060-easy-unshift) Here is my code:
type Unshift<T extends unknown[], U> = U extends unknown[]
? [...U, ...T]
: [U, ...T]
the problem is with boolean type:
type Res = Unshift<['1', 2, '3'], boolean>
// expected: [boolean,'1',2,'3']
// actual: [false,'1',2,'3'] | [true,'1',2,'3']
I don't know why is that and how to get expected result. BTW, I got a pretty similar solution from another answer(Typescript spread operator for type),
type Cons<H, T extends readonly any[]> =
((h: H, ...t: T) => void) extends ((...r: infer R) => void) ? R : never
is it a trick that should be rote?
type Res = Unshift<['1', 2, '3'], "a" | "b">you get the same kind of result. EDIT: look at this: tsplay.dev/mZrx9m i think this has to do with the compiler trying to do discriminated unions[false,'1',2,'3'] | [true,'1',2,'3']different from[boolean,'1',2,'3']in any meaningful way?