There is a general function in my code that I have simplified to the example below:
type GenericDelegate<Type extends "firstType" | "secondType"> = {
type: Type;
deleteItems: (query: { query: { [n in Type]: string } }) => Promise<unknown>;
};
const removeDuplicates = <Type extends "firstType" | "secondType">(
genericDelegate: GenericDelegate<Type>
) => {
genericDelegate.deleteItems({ query: { [genericDelegate.type]: 'item' } });
};
Since type
is of the type Type
, it should not pose any issues when passing it to query
. However, TypeScript throws an error:
Type '{ [x: string]: string; }' is not assignable to type '{ [n in Type]: string; }'.
Do you know why [genericDelegate.type]
is being interpreted as x: string
rather than x: Type
?