-
-
Notifications
You must be signed in to change notification settings - Fork 5.1k
Open
Description
3062 - Shift
Sift is just a simple reimplementation of Array.shift: it just removes the first item of the array. Here, though, we do it all in TypeScript's type system itself.
🎥 Video Explanation
🔢 Code
// ============= Test Cases =============
import type { Equal, Expect } from './test-utils'
type A1 = Shift<[]>;
type B1 = [];
type C1 = Expect<Equal<A1, B1>>;
type A2 = Shift<[1]>;
type B2 = [];
type C2 = Expect<Equal<A2, B2>>;
type A3 = Shift<[3, 2, 1]>;
type B3 = [2, 1];
type C3 = Expect<Equal<A3, B3>>;
type A4 = Shift<['a', 'b', 'c', 'd']>;
type B4 = ['b', 'c', 'd'];
type C4 = Expect<Equal<A4, B4>>;
// @ts-expect-error(2344)
type E1 = Shift<unknown>;
// ============= Your Code Here =============
type Shift<T extends unknown[]> =
// also, `any` or `infer _` instead of `unknown`
T extends [unknown, ...infer Tail]
? Tail
: [];➕ More Solutions
For more video solutions to other challenges: see the umbrella list! #21338
Metadata
Metadata
Assignees
Labels
No labels