Filter falsy values from an array with type safety
Using flatMap or ts-reset to remove falsy values from array with type safety
Using flatMap
If we have
and we want to filter the falsy values we can do
or
and that works good, but, if we are working with typescript we will see that the bad type:
To fix it, we can the array method flatMap
: is the combination of a map and a flat operation
or shorter:
and the type is
Using ts-reset
ts-reset offers extremely strict TypeScript:
Using the first example would work