For my new small project, I decided to go with typescript for some reason no better than making myself feel like c programming on wonderful steroids and apparently I am paying a price.
I have an interface on the right
export default interface DataObject {
[key: string]: any
};
This supposedly allows me to define objects with string keys and any values. Then i implement it
import DataObject from "./data-object";
export default class Model implements DataObject {
constructor(data: DataObject = {}) {
Model.fill(this, data);
}
static fill(model: Model, data: DataObject) {
for (let prop in data) {
model[prop] = data[prop];
}
}
}
Now i get this error
An element is implicitly of type "any" because the type of "Model" has no index signature
in this line
model[prop] = data[prop];
But if I modify my model to include a signature
import DataObject from "./data-object";
export default class Model implements DataObject {
[key: string]: any;
...
}
Then there is no mistake.
Why does the interface signature not affect my class?
source
share