Skip to content

Allow Boolean(val) && to refine val the same as val && #45601

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

Closed
5 tasks done
ghost opened this issue Aug 27, 2021 · 2 comments
Closed
5 tasks done

Allow Boolean(val) && to refine val the same as val && #45601

ghost opened this issue Aug 27, 2021 · 2 comments
Labels
Duplicate An existing issue was already created

Comments

@ghost
Copy link

ghost commented Aug 27, 2021

Suggestion

πŸ” Search Terms

boolean refinement

βœ… Viability 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, new syntax sugar for JS, etc.)
  • This feature would agree with the rest of TypeScript's Design Goals.

⭐ Suggestion

Boolean(x) && x.y should be identical to !!x && x.y as far as the type system is concerned

πŸ’» Use Cases

use case: boolean return value along with style guides that promote Boolean(val) vs !!val

function foo(x: {y: boolean} | null): boolean {
    return !!x && x.y;
}

function foo2(x: {y: boolean} | null): boolean {
    return Boolean(x) && x.y; // error on x.y, x is possibly null
}

The one issue I could theoretically see is someone weirdly overwriting Boolean and introducing side-effects. But talk about edge cases!

I can re-write this as Boolean(x?.y), but that's not as clear IMO.

@IllusionMH
Copy link
Contributor

Duplicate of #16655, #37180 and some other.
Can be found by Boolean type guard

@andrewbranch andrewbranch added the Duplicate An existing issue was already created label Aug 27, 2021
@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

3 participants