I have successfully created a "load" function that takes JSON data and loads the attributes into properties of my class. As I am experimenting with typescript for the first time, I managed to resolve most errors but there are a few remaining challenges.
load(id): void{
let item:object = this.items.find(item => item.id == id);
if(typeof(item) != 'undefined'){
for(let k in item){
if(item.hasOwnProperty(k)){
this[k] = item[k];
}
}
}
}
The code snippet above leads to the following error:
Element implicitly has an 'any' type because expression of type 'string' can't be used to index type '{}'.
No index signature with a parameter of type 'string' was found on type '{}'.
386 | for(let k in item){
387 | if(item.hasOwnProperty(k)){
> 388 | this[k] = item[k];
| ^
389 | }
390 | }
391 | }
How can I fix this issue? Is it related to the pattern I'm using, or should I declare the types differently to prevent the error from occurring?