This solution is generated by ChatGPT, correctness is not guaranteed. You might also be able to find a solution fromcommunity posts.
AI solution for TypeScript puzzle on BFE.dev
29. implement Shift<T>
type Shift<T extends any[]> = T extends [any, ...infer Rest] ? Rest : [];type A = Shift<[1,2,3]>; // [2, 3]type B = Shift<[1]>; // []type C = Shift<[]>; // []
The Shift
type takes in a tuple T
and checks if the tuple contains at least one element (T extends [any, ...infer Rest]
). If it does, it returns the rest of the elements using the infer
keyword to capture the type of the remaining elements. If the tuple is empty or only contains one element, it returns an empty tuple using the built-in []
type.