Skip to content

Prepending true || confuses strict null checks, even with correct bracketing #31855

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
robinmessage opened this issue Jun 11, 2019 · 2 comments
Closed

Comments

@robinmessage
Copy link

robinmessage commented Jun 11, 2019

OR-ing an expression which passes type checking with true causes that expression to fail type checking.

TypeScript Version: 3.6.0-dev.20190611

Search Terms: boolean logic strict null check confused or with true

Code

type Foo = { bar?: { baz?: string } };
const foo = (something: Foo) => (something.bar && something.bar.baz);
const quux = (something: Foo) => true || (something.bar && something.bar.baz);

Expected behavior:
foo and quux both pass type checking with strict null checks.

Actual behavior:
foo is fine, but tsc complains about something.bar.baz in quux. It is almost like it interprets it as (true || something.bar) && something.bar.baz which would indeed be unsafe.

Playground Link: https://www.typescriptlang.org/play/#src=type%20Foo%20%3D%20%7B%20bar%3F%3A%20%7B%20baz%3F%3A%20string%20%7D%20%7D%3B%0D%0Aconst%20foo%20%3D%20(something%3A%20Foo)%20%3D%3E%20(something.bar%20%26%26%20something.bar.baz)%3B%0D%0Aconst%20quux%20%3D%20(something%3A%20Foo)%20%3D%3E%20true%20%7C%7C%20(something.bar%20%26%26%20something.bar.baz)%3B%0D%0A
— N.B. it only fails with strict null checks turned on

Related Issues: I did not find any.

@nmain
Copy link

nmain commented Jun 11, 2019

Duplicate of #26914

@robinmessage
Copy link
Author

Thanks, it didn't occur to me that the code was unreachable and that was why it didn't work, so I didn't see the other ticket.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

No branches or pull requests

2 participants