@cuppachino/type-space
Version:
A collection of type utilities for TypeScript.
17 lines (16 loc) • 759 B
TypeScript
import type { Subtract } from '../math/subtract';
import type { UnknownArray } from '../unknown-array';
/**
* Removes the first `N` elements from a tuple type. Does not return the removed types.
*
* @remarks Its worth noting `Pop`, `Push`, `Shift`, and `Unshift` are inspired by JS; however, the types are not 1:1 for design reasons. More information can be found in [README](
* @example
* ```
* declare const tuple: ['d', 'a', 'v', 'i', 'd']
* type Tuple = ShiftN<typeof tuple, 2> // ['v', 'i', 'd']
* ```
*/
export type ShiftBy<T extends UnknownArray, N extends number> = T extends [
any,
...infer U
] ? N extends 0 ? T : ShiftBy<U, Subtract<N, 1>> : T extends readonly [any, ...infer U] ? N extends 0 ? T : ShiftBy<U, Subtract<N, 1>> : T;