Skip to content

Make type assumptions when using Boolean function #42572

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
strblr opened this issue Feb 1, 2021 · 1 comment
Closed
5 tasks done

Make type assumptions when using Boolean function #42572

strblr opened this issue Feb 1, 2021 · 1 comment
Labels
Duplicate An existing issue was already created

Comments

@strblr
Copy link

strblr commented Feb 1, 2021

Suggestion

Right now, only the last instruction fails in TS with the error Object is possibly 'null' :

declare const value: null | { x: number };

const foo = value && value.x;
const bar = !!value && value.x;
const baz = Boolean(value) && value.x;

Playground

Since it's strictly equivalent to the instruction just above, I suggest making the same type assumptions.

🔍 Search Terms

Boolean, function, inference, assumption, assertion

✅ 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.
@MartinJohns
Copy link
Contributor

Duplicate of #16655.

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