I’ve used the following type:
type FixedSizeArray<N extends number, T> = {
length: N
} & ReadonlyArray<T>
But then TS doesn’t accept
someProp: Array.from({ length: 16 }).map(() => '')
where someProp
expects FixedSizeArray<16, string>
.
I either have to cast it or write ''
16 times. The actual error thrown being:
Type 'string[]' is not assignable to type 'FixedSizeArray<16, string>'.
Type 'string[]' is not assignable to type '{ length: 16; }'.
Types of property 'length' are incompatible.
Type 'number' is not assignable to type '16'.ts(2322)
See this answer for implementation details.
Is this a TS bug or am I doing something wrong?
1