Skip to content
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

Closed
5 tasks done
mmkal opened this issue Mar 27, 2019 · 4 comments
Closed
5 tasks done

narrow array type with .filter(Boolean) #30621

mmkal opened this issue Mar 27, 2019 · 4 comments
Labels
Duplicate An existing issue was already created

Comments

@mmkal
Copy link

mmkal commented Mar 27, 2019

Search Terms

narrow, array, filter

Suggestion

add this overload to Array<T>.filter:

filter(fn: typeof Boolean): Array<Exclude<T, null | undefined | 0 | ''>>

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 with strictNullChecks.

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

const maybeStrings = ['foo', null, 'bar']
const definitelyStrings = maybeStrings.filter(Boolean)

definitelyStrings is of type (string | null)[] even though it's impossible for nulls to be in it.

current behaviour:
image

proposed behaviour:
image

Checklist

My suggestion meets these guidelines:

  • This wouldn't be a breaking change in existing TypeScript/JavaScript code (I don't think so - types would become more specific)
  • This wouldn't change the runtime behavior of existing JavaScript code
  • This could be implemented without emitting different JS based on the types of the expressions
  • This isn't a runtime feature (e.g. library functionality, non-ECMAScript syntax with JavaScript output, etc.)
  • This feature would agree with the rest of TypeScript's Design Goals.
@jack-williams
Copy link
Collaborator

See #29955

@mmkal
Copy link
Author

mmkal commented Mar 28, 2019

@jack-williams interesting, it'd be great if this could be solved by modifying the type of Boolean rather than filter. But I just tried manually putting in @ForbesLindesay 's signature in typescript@next and it doesn't allow using .filter(Boolean) without explicitly supplying the generic i.e. .filter<string>(Boolean). That's fine-ish for strings but when it's a complex inferred type it wouldn't be feasible.

@DanielRosenwasser DanielRosenwasser added the Duplicate An existing issue was already created label Mar 28, 2019
@typescript-bot
Copy link
Collaborator

This issue has been marked as a 'Duplicate' and has seen no recent activity. It has been automatically closed for house-keeping purposes.

@ForbesLindesay
Copy link
Contributor

@mmkal That can be fixed, but will require improvements to the inference engine.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
Duplicate An existing issue was already created
Projects
None yet
Development

No branches or pull requests

5 participants