Challenge

Implement the type version of Array.shift(). For example:

type Result = Shift<[3, 2, 1]>; // [2, 1]

Solution

Another challenge focused on manipulating the tuples. In this case, we need to split the element from the beginning of the tuple and the “other” part - tail. We start with the initial type:

type Shift<T> = any;

In order to get the first element of the tuple and the rest of it, we can use inferring within conditional types:

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

The first element is named as _ because we don’t care about it. However, we care about the rest of the tuple, which is exactly a shifted one. So that, we return it from the true branch of the conditional type:

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

When type parameter T is not matched against the pattern with a head and a tail, return the empty tuple, because no need to shift:

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

References