Closed
Description
TypeScript Version: > 3.5.1, 3.6.0-dev.20190606
Search Terms:
Type 'string' cannot be used to index type for generic type
Code
function set_extends<
T extends {
[key: string]: string;
}
>(obj: T, key: string, value: string) {
obj[key] = value; // <-- error TS2536: Type 'string' cannot be used to index type 'T'.
}
const set_union = <T>(
obj: T & { other: string; [key: string]: string },
key: string,
value: string
) => {
obj[key] = value; // <-- error TS2536: Type 'string' cannot be used to index type 'T & { [key: string]: string; other: string; }'.
};
function set_works<T>(obj: T, key: keyof T, value: T[keyof T]) {
obj[key] = value;
}
Expected behavior:
No compilation errors.
Actual behavior:
Compilation errors for set_extends
:
Type 'string' cannot be used to index type 'T'
Compilation errors for set_union
:
Type 'string' cannot be used to index type 'T & { [key: string]: string; other: string; }'
NOTE: The TypeScript version 3.4.5 gives no compilation errors
Playground Link:
NOTE: Not sure the playground is running latest TypeScript yet, so cannot be reproduced there
Related Issues:
#21760