Description
TypeScript Version: 3.7.0-dev.20191018
Search Terms: keyof string index record not assignable
Code
function test<T extends { [key: string]: string }>(map: T) {
const key: keyof T = "hello"
map[key] = "hi there"
}
(see playground for a more real example)
Expected behavior: Infer that the types accepted by the function will allow string key assignment (to string value). Since an interface with members incompatible with the string->string signature is invalid, anything that extends Record<string, string>
should also have string keys with string values.
Actual behavior: An error such as:
Type 'string' is not assignable to type 'T[keyof T]'.
Type 'string' cannot be used to index type 'T'.
Type 'string' is not assignable to type 'T[Extract<keyof T, string>]'.
The last one is if you attempt to do the assignment inside of a for (const key in map)
loop. In this case especially, you'd think that even if there were somehow other properties with other types on the object (eg symbol or number indices), that it's narrowed down to just strings.
Related Issues: #29295 was similar but possible to work around.