According to the information found in the TypeScript documentation on mapped tuple types, if a fixed-length tuple type is created as follows:
type FixedList = [string, number, boolean];
and then a generic type intended to be passed into it is created:
type SomeGeneric<T> = {
propOne: number;
propTwo: string;
propThree(arg0: T): void;
};
An attempt should be made to create a mapped object type
type GenericFixedList = {
[K in keyof FixedList]: SomeGeneric<FixedList[K]>;
};
Then, when working with a correctly-sized array, the following should work:
const stronglyTypedList: GenericFixedList = [
{
propOne: 0,
propTwo: "zero",
propThree(arg0) {},
},
// ... etc
];
However, while attempting this, an error is encountered:
... is not assignable to type GenericFixedList:
types of property 'length' are incompatible.
Type 'number' is not assignable to type SomeGeneric<3>.
This indicates that despite the documentation mentioning that length
should be disregarded when constructing the tuple type, the array length of 3 from keyof FixedList
somehow gets passed to the mapped object type, resulting in the type constraint length: SomeGeneric<3>
, which is naturally incompatible with an array.
An attempt was made to specify
Omit<FixedList, "length">
but the issue just shifted to the next array property (the iterator function for for in
/for of
).
The question remains, what exactly is causing this issue?