-
-
Notifications
You must be signed in to change notification settings - Fork 228
Open
Labels
Description
Is your feature request related to a real problem or use-case?
Add a generic way to specify length of a tuple type
Describe a solution including usage in code example
// From @ahejlsberg PR https://github.com/microsoft/TypeScript/pull/40002
type TupleOf<T, N extends number> = N extends N ? number extends N ? T[] : _TupleOf<T, N, []> : never;
type _TupleOf<T, N extends number, R extends unknown[]> = R['length'] extends N ? R : _TupleOf<T, N, [T, ...R]>;
type Vector<Length extends number> = TupleOf<number, Length>
type Matrix<Rows extends number, Columns extends number> = TupleOf<TupleOf<number, Columns>, Rows>
const v: Vector<2> = [1, 2]
const m: Matrix<2, 3> = [
[1, 2, 3],
[1, 2, 3],
]