I am relatively new to TypeScript and recently encountered an issue while working with classes.
class DataStorage {
private data:string[] = [];
addItem(item: string){
this.data.push(item);
}
removeItem(item: string){
this.data.splice(this.data.indexOf(item), 1);
}
getItems(): string[]{
return [...this.data];
}
// the following code is causing an error
getConcatedItems(){
const arr:string[] = [];
return arr.concat[this.data];
}
}
When using this.data
in the function getConcatedItems
, I received an error message stating that
Type 'string[]' cannot be used as an index type.
. Surprisingly, no error was displayed for the function getItems
.
What could be the reason behind this discrepancy and how can this issue be resolved?