-
Notifications
You must be signed in to change notification settings - Fork 12.4k
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
narrow array type with .filter(Boolean)
#30621
Comments
See #29955 |
@jack-williams interesting, it'd be great if this could be solved by modifying the type of |
This issue has been marked as a 'Duplicate' and has seen no recent activity. It has been automatically closed for house-keeping purposes. |
@mmkal That can be fixed, but will require improvements to the inference engine. |
Search Terms
narrow, array, filter
Suggestion
add this overload to
Array<T>.filter
:This may be considered too specific - but the use case is very common so hopefully worth it.
Use Cases
This will allow using
array.filter(Boolean)
to get rid of null/undefined/falsy values, and have the type system track it correctly even withstrictNullChecks
.Right now the options are to use
lodash.compact
or write a similar function (not ideal to add a dependency), or to use!
(myArray.filter(Boolean).map(val => val!.foo(...))
) which is a little dangerous.Examples
Example
definitelyStrings
is of type(string | null)[]
even though it's impossible fornull
s to be in it.current behaviour:
proposed behaviour:
Checklist
My suggestion meets these guidelines:
The text was updated successfully, but these errors were encountered: