Jeffrey Haskell Jeffrey Haskell - 3 years ago 121
TypeScript Question

forEach Typescript TS2339 "does not exist on type 'void'"

Trying to find the intersection of two arrays, why am I getting

TS2339:Property 'collection' does not exist on type 'void'
?

All arrays are declared in the same class.

this.locations.forEach(function(location) {
this.collection.locations.forEach(function(id) {
if(location._id === id) {
this.display.push(location);
}
});
});

Answer Source

Using a function takes the this from the caller (something in forEach in your case); using => takes the this from the outer scope. Thus use:

this.locations.forEach(location => {
    this.collection.locations.forEach(id => {
        if(location._id === id) {
            this.display.push(location);
        }
    });
});

I recommend the following readings:

Recommended from our users: Dynamic Network Monitoring from WhatsUp Gold from IPSwitch. Free Download