Skip to content

Instantly share code, notes, and snippets.

@uhyo
Created June 4, 2019 13:46
Show Gist options
  • Save uhyo/5999a847fcfe34ba116c5d22b4b52bbd to your computer and use it in GitHub Desktop.
Save uhyo/5999a847fcfe34ba116c5d22b4b52bbd to your computer and use it in GitHub Desktop.
Array with least number of elements
type AtLeast<N extends number, T> = AtLeastRec<N, T, T[], []>;
type AtLeastRec<Num, Elm, T extends unknown[], C extends unknown[]> = {
0: T;
1: ((arg: Elm, ...rest: T) => void) extends ((...args: infer T2) => void)
? ((arg: unknown, ...rest: C) => void) extends ((...args: infer T3) => void)
? AtLeastRec<Num, Elm, T2, T3>
: never
: never;
}[C extends { length: Num } ? 0 : 1];
const arr: AtLeast<3, number> = [0, 1];
const arr2: AtLeast<3, number> = [0, 1, 2];
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment