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

Add type predicate to Boolean (BooleanConstructor) #32834

Closed
5 tasks done
KSXGitHub opened this issue Aug 13, 2019 · 2 comments
Closed
5 tasks done

Add type predicate to Boolean (BooleanConstructor) #32834

KSXGitHub opened this issue Aug 13, 2019 · 2 comments
Labels
Duplicate An existing issue was already created

Comments

@KSXGitHub
Copy link
Contributor

Suggestion

Add an overload to Boolean defintion

 interface BooleanConstructor {
     new(value?: any): Boolean;
-    <T>(value?: T): boolean;
+    <T>(value?: T): value is Exclude<T, false | null | undefined | 0 | ''>
     readonly prototype: Boolean;
 }

playground

Use Cases

Array.filter

declare const actions: Array<(() => void) | null | undefined>
actions.filter(Boolean).forEach(act => act())

Checklist

My suggestion meets these guidelines:

  • This wouldn't be a breaking change in existing TypeScript/JavaScript code
  • 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.
@MartinJohns
Copy link
Contributor

Duplicate of #31164, #24063, #16655. Was implemented by #29955, and broken by #31515.

@RyanCavanaugh RyanCavanaugh added the Duplicate An existing issue was already created label Aug 14, 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.

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

4 participants