How to filter out undefined from array in ts?
- typescript
- filter-undefined
- tips
- use-queries-filter

So you want to filter out undefined from an array
But it keeps telling you that the result is an union of undefined | T
.
You might be tempted to use filter + map combo and a type guard along the way.
For a real world scenario let's use react-query.
It would roughly look like this.
Some starter code for react query.
Let's create queries based on ids from 1 to 10.
Let's get only defined todos from the queries the usual way
Let's see now how reduce can work in our favor here
Performance concerns
For those who would be on the border here; let me explain myself.
I was not able to get constant results using the performance tab, but reduce version seemed to be around 0.1 ms slower when using 6x throttle (usually).
It's probably connected to the fact that each time a new place in memory has to be created for the brand new array.
On the other hand such concerns are rarely a thing for product devs'.
Let's face it a vast majority of time taken scripting in our apps is framework code.
Conclusion
My personal preference is the reduce way. Choose whichever way you prefer.
Remember!
Premature optimization is not what your employer wants you to take care of, your time is worth a lot 🐰.