@cuppachino/type-space
Version:
A collection of type utilities for TypeScript.
17 lines (16 loc) • 764 B
TypeScript
import type { Subtract } from '../math/subtract';
import type { UnknownArray } from '../unknown-array';
/**
* Removes the last `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 = PopN<typeof tuple, 2> // ['d', 'a', 'v']
* ```
*/
export type PopBy<T extends UnknownArray, N extends number> = T extends [
...infer U,
any
] ? N extends 0 ? T : PopBy<U, Subtract<N, 1>> : T extends readonly [...infer U, any] ? N extends 0 ? T : PopBy<readonly [...U], Subtract<N, 1>> : T;