Joshua Ohana Joshua Ohana - 4 months ago 23
TypeScript Question

TypeScript typed array declaration error

For a given class I am declaring a few private variables in the constructor, and making some calls. I can getting a No Provider error on my typed array declaration when including it in the constructor, and no errors when declaring it outside the constructor.

This code works, error free

private data: Array<MyType>;

constructor(private service: Service) {
this.service.getData().then(response => this.data = response.json());
}


Then, paradoxically, the below does not work, and produces the error found below the snippet.

constructor(private data: Array<MyType>, private service: Service) {
this.service.getData().then(response => this.data = response.json());
}


Error from console is


EXCEPTION: Error: Uncaught (in promise) ORIGINAL EXCEPTION: No provider for Array!


Is this some syntax error? Am I horribly misunderstanding a concept? Both implementations appear identical and feel like they should behave exactly the same.

Answer

The code is equivalent... except for the call to the constructor. ;)

In the first case:

new TheClass(service);

In the second case:

new TheClass(somethingHere, service);