FixedLengthArray
could be improved to be stricter. Take this snippet for example:
const array: FixedLengthArray<number, 3> = [1, 2, 3];
array[4];
The final line should not compile since index 4
doesn't exist on the array.
Instead a type something like this could be used:
type Helper<Element, Length extends number, Rest extends Element[]> = Rest['length'] extends Length ? Rest : Helper<Element, Length, [Element, ...Rest]>;
export type FixedLengthArray<Element, Length extends number> = Length extends Length ? (number extends Length ? Element[] : Helper<Element, Length, []>) : never;
This uses TypeScript's tuple types directly instead of [index: number]: Element
which is much safer.
Playground for current behavior
Playground for proposed behavior
Pay now to fund the work behind this issue.
Get updates on progress being made.
Maintainer is rewarded once the issue is completed.
You're funding impactful open source efforts
You want to contribute to this effort
You want to get funding like this too