Created
June 4, 2019 13:46
-
-
Save uhyo/5999a847fcfe34ba116c5d22b4b52bbd to your computer and use it in GitHub Desktop.
Array with least number of elements
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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