One of my programming utilities allows me to create a statically sized array:
export type StaticArray<T, L extends number, R extends any[] = []> = R extends {
length: L;
}
? R
: StaticArray<T, L, [...R, T]>;
To verify its functionality, I can use it like this:
let myVar: StaticArray<number, 3>;
// ^? let myVar: [number, number, number]
Now, I am interested in making it work with sum types, but I'm unsure on how to achieve that. I envision the following outcome for this scenario:
let myVar: StaticArray<number, 3 | 2 | 1>;
// ^? let myVar: [number, number, number] | [number, number] | [number]
Unfortunately, my attempts have been unsuccessful as the StaticArray
type seems to stop at the smallest number encountered.