Skip to content

Language Service type semantics are not always accounting for logical AND operator. #18182

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
IEvangelist opened this issue Aug 31, 2017 · 5 comments
Labels
Design Limitation Constraints of the existing architecture prevent this from being fixed

Comments

@IEvangelist
Copy link
Member

TypeScript Version: 2.2.2.0

Code

const dateStr: string = "";
const date = dateStr && new Date(dateStr);

I recreated this issue in the playground too.

Expected behavior:

I'd expect that the language service powering the IDE would be able to determine that the variable namely date is actually a union type of string | Date.

Actual behavior:

The IDE claims that date variable is actually a type of Date, ignoring the possibility that the left operand of the logical AND might evaluate as false. In the above snippet the type of date is actually a string as the short-circuit occurs, returning the value of the left operand (according to the

I'm using version 2.2.2.0, but the playground exhibits this same behavior (although, I'm not certain what version it uses).

@mhegazy
Copy link
Contributor

mhegazy commented Aug 31, 2017

Under --strictNullChecks logical operators should have the expected type, see #8949 for more details.

@mhegazy mhegazy added the Design Limitation Constraints of the existing architecture prevent this from being fixed label Aug 31, 2017
@IEvangelist
Copy link
Member Author

The playground still thinks it is a Date even after toggling strictNullChecks on.
image
After toggle and tooltip hover of date.
image

@mhegazy
Copy link
Contributor

mhegazy commented Aug 31, 2017

this type is any in the global scope. so this.dateStr is any as well. remove this or select noImplictThis to see the error.

@IEvangelist
Copy link
Member Author

Thank you, @mhegazy!

@mhegazy
Copy link
Contributor

mhegazy commented Sep 18, 2017

Automatically closing this issue for housekeeping purposes. The issue labels indicate that it is unactionable at the moment or has already been addressed.

@mhegazy mhegazy closed this as completed Sep 18, 2017
@microsoft microsoft locked and limited conversation to collaborators Jun 14, 2018
Sign up for free to subscribe to this conversation on GitHub. Already have an account? Sign in.
Labels
Design Limitation Constraints of the existing architecture prevent this from being fixed
Projects
None yet
Development

No branches or pull requests

2 participants