0

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?

2
  • 2
    Because it needs to be arr.concat(this.data); Commented May 8, 2021 at 19:25
  • 1
    You used square brackets to call a function Commented May 8, 2021 at 19:25

1 Answer 1

2

It's just misspelling. Should be

return arr.concat(this.data);

So round brackets instead of square ones.

Sign up to request clarification or add additional context in comments.

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.