1

In my Angular app I have a filter function that keeps track of both user inputed filter values, and whether or not those filter values are currently enabled/active. I am initializing these filters like so:

filters = { language: [], location: [], zipArray: [], firstName: [], lastName: [] };

I am running into a Typescript error with this section of code -- specifically this line: return this.filters.zipArray = [];

public onZipcodeEnabledChange(enabled): void {
    this.filters.zipArray = this.getZipcodeArray();
    if (!enabled || this.filters.zipArray && this.filters.zipArray[0] === ''){
        return this.filters.zipArray = [];
    }
}

The TypeScript error I'm getting is:

Type 'undefined[]' is not assignable to type 'void'.

I'm not understanding what the issue is here?

2
  • 3
    You have the return type of onZipcodeEnabledChange as void, but you're returning an empty array. Try writing return; on its own line after you set the zipArray filters. Commented Apr 18, 2018 at 16:49
  • void means it doesnt return anything and you have 2 issues 1 you're returning an assignment and two you're returning something if you want to resolve : string[] Commented Apr 18, 2018 at 16:50

1 Answer 1

8

You declared return type of onZipcodeEnabledChange as void it means that function will not return anything. And in if statement, you are returning assignment result of return this.filters.zipArray = []; i.e. [].

Solution 1

Just remove return keyword will work for you.

public onZipcodeEnabledChange(enabled): void {
    this.filters.zipArray = this.getZipcodeArray();
    if (!enabled || this.filters.zipArray && this.filters.zipArray[0] === ''){
       this.filters.zipArray = [];
    }
}

Solution 2

And if you want to return array from that function, you need to replace void with any[].

public onZipcodeEnabledChange(enabled): any[] {
    this.filters.zipArray = this.getZipcodeArray();
    if (!enabled || this.filters.zipArray && this.filters.zipArray[0] === ''){
       this.filters.zipArray = [];
       return this.filters.zipArray;
    }
}
Sign up to request clarification or add additional context in comments.

5 Comments

and vis vera if you do want to return you'll have to change void to : any[] or string[] whatver the type the stuff in the array
Great. Thanks. Will make this change.
@JoeWarner, Absolutely, I updated the answer. Thanks :)
lol i was typing my answer out you beat me too it xD good stuff
With Solution 2, like @JoeWarner mentioned, I would suggest typing the method return type instead of using any whilst also typing the properties here, you could initialise this something like zipArray: ['value1'] with the values, also I would recommend moving the return outside of the if block, so both paths return the same type for readability and this methods caller.

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.