I am attempting to set a value for the property of an object with generic typing passed into a function. The structure of the object is not known beforehand, and the function receives the property name dynamically as a string argument. TypeScript is generating the error "Type 'string' cannot be used to index type 'T'."
My TypeScript code looks like this:
interface Value {
[key: string]: any
}
interface SomeInterface<T> {
key: string,
value: T
}
function someFunction<T extends Value>({ key, value }: SomeInterface<T>) {
value[key] = somevalue;
}
The line where I assign somevalue to the value triggers the error "Type 'string' cannot be used to index type 'T'", despite having created the index signature in the Value interface.
What could be the reason behind this issue?