In the following code, TypeScript seems to flippantly decide when a type is indexable by a string:
type Model = { [k: string]: any };
export class MyClass<TModel extends Model> {
get(target: TModel, key: string) {
return target[key]; //passes
}
set(target: TModel, key: string, value: any) {
target[key] = value; //ts(2536) Type 'string' cannot be used to index type 'TModel'
}
}
TypeScript version: 4.7.2.
I've been bumping into this issue a lot lately and I'm wondering if anyone knows why.