Is it possible to define a type foo
following this pseudo-code?
type foo<T> = {
length: number;
array: T[length]
}
Alternatively:
type foo<T> = {
length: array.length;
array: T[]
}
For example, {length: 2, array: ["a","b"]}
should have this type, but {length: 3, array: ["a","b"]}
should not.
It has been answered before how to define a FixedLengthArray<T,N extends number>
type. We can use this to define:
type foo<T,N extends number> = {
length: N;
array: FixedLengthArray<T,N>
}
This works when I want to define
let bar : foo<string,2> = {length: 2, array: ["a","b"]};
But my goal is to write
let bar : foo<string> = {length: 2, array: ["a","b"]};
Can we somehow let TypeScript infer the length?