I have the following code:
interface MyType {
a: string;
b: number;
}
class SomeClass {
// ... more things going on
beta: MyType = {a: 'Lorem', b: 3};
myFunction(alpha: MyType) {
// beta is an object of type MyType
for (const key in alpha) {
this.beta[key as keyof typeof this.beta] = alpha[key as keyof typeof alpha] ?? '';
}
}
}
see identical playground here.
However, this produces the error:
Element implicitly has an 'any' type because expression of type 'string | number | symbol' can't be used to index type 'MyType'.
No index signature with a parameter of type 'string' was found on type 'MyType'.
I've tried playing around with it for a long time, tweaking it, and it still doesn't work -- I've no idea why. (I'm using keyof typeof this.beta
for simplicity but keyof MyType
doesn't work either).
Thanks for the help in advance.