Green Wizard
Green Wizard

Reputation: 3657

Why is Array concatenation throwing an error in TypeScript?

I'm quite new to TypeScript. I was experimenting with classes and happened to see an error.

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];
    }

    // below code gives an error
    getConcatedItems(){
        const arr:string[] = [];
        return arr.concat[this.data];
    }
}

In the above code this.data present in getConcatedItems is showing an error that says Type 'string[]' cannot be used as an index type.. While the getItems does not show any error.

Why would this happen and what could be a resolution for this?

Upvotes: 0

Views: 485

Answers (1)

Konrad
Konrad

Reputation: 287

It's just misspelling. Should be

return arr.concat(this.data);

So round brackets instead of square ones.

Upvotes: 2

Related Questions