amazingCarrotSoup amazingCarrotSoup - 11 days ago 92
TypeScript Question

types typescript inside for loop local loop variable not respecting type

this.venuelist has type Venue

venuelist:Venue[] = [];


Type
Venue
has the property
neighborhood
on it.

I have the following for loop

for(let venue in this.venuelist){
let remove = false;
if(this.filters.neighborhood != ''){
if(venue.neighborhood != this.filters.neighborhood){
remove = true;
}
}
}

calling venue.neighborhood called in the second `if statement` is not working


the error is: Property 'neighborhood' does not exist on type 'string.

for why? Why must it do this. Why can't it play nice? How do I make it place nice?

Answer Source
for(let venue in this.venuelist){}

for...in doesn't iterate over the array items, it iterates over the keys of the object passed in. You should use for...of