I got this error:
Type '{ [key: string]: any; }' is not assignable to type 'T'.
'{ [key: string]: any; }' is assignable to the constraint of type 'T', but 'T' could be instantiated with a different subtype of constraint '{ [key: string]: any; }'.(2322)
from this piece of code:
function getValue ():{[key: string]: any} {
return {key:'value'}
}
class Foo<T extends {[key: string]: any}> {
public readonly data?: T
constructor() {
this.data = getValue()
}
}
Does anyone know why and how to solve this error?
Are you looking to store a dictionary of items with type T? Then maybe this is what you want?: