-
Notifications
You must be signed in to change notification settings - Fork 12.8k
Allow Boolean() to be used to perform a null check #29955
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
Allow Boolean() to be used to perform a null check #29955
Conversation
Hi, this is a really good initiative, do you think we can skip specifying generic after this |
@typescript-bot test this |
Heya @RyanCavanaugh, I've started to run the extended test suite on this PR at 24e7e1c. You can monitor the build here. It should now contribute to this PR's status checks. |
@beshanoe I think that may be sufficient to allow us to skip specifying generic parameters. I'm not sure though without testing it. I found: function B<T>(value: T): value is Exclude<T, false | null | undefined | '' | 0> {
return Boolean(value);
} was already sufficient, but adding prototypes/new functions to the interface forces you to be explicit. |
@ForbesLindesay can you merge this up with master? This will allow us to re-run the RWC suite to evaluate the effects of this on our real-world code database |
… boolean-null-check
@RyanCavanaugh sorry I didn't see your message. I've merged it now. |
@RyanCavanaugh any update on this? |
@typescript-bot test this |
Heya @RyanCavanaugh, I've started to run the extended test suite on this PR at 37bb42c. You can monitor the build here. It should now contribute to this PR's status checks. |
RWC is actually clean, ignore ❌ |
@RyanCavanaugh any chance we can merge this? It's been a while and it's a very simple fix. |
☝️ 🙏 |
Resolves #31164. |
broken by #31515 |
This would be a great addition to the language. Have there been any plans on when it might land? 3.8 maybe? |
Can we revive this. Still continues to prevent usage of the .filter(Boolean) syntax sugar today. |
Check out ts-reset. |
Is there a good workaround in the meantime? I'm not sure how else to express |
You can use type guards: ["", undefined, "test"].filter((x): x is string => typeof x === "string") |
Works universally for any |
The code in #29955 (comment) more completely shows the result of |
Fixes #16655
This allows code like:
to be written and to typecheck. Unfortunately the generic type still has to be specified, otherwise TypeScript picks the other, less specific overload of
.filter
.