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

TypeScript Compiler does not understand resulting types when using && #12174

Closed
Ristaaf opened this issue Nov 11, 2016 · 3 comments
Closed

TypeScript Compiler does not understand resulting types when using && #12174

Ristaaf opened this issue Nov 11, 2016 · 3 comments
Labels
Fixed A PR has been merged for this issue

Comments

@Ristaaf
Copy link

Ristaaf commented Nov 11, 2016

TypeScript Version: 2.0.8

Code

// A *self-contained* demonstration of the problem follows...
let s:string=("" && false);

Expected behavior:
Since ("" && false) is "" in JavaScript I should be able to assign the resulting value to a string, but TypeScript thinks that this will become a boolean.
Actual behavior:
Compiler error claiming that a boolean cannot be assigned to a string

Disclaimer: I think that the behaviour of the && operator in JavaScript is horrible, however it is what it is and TypeScript should honour it.

@RyanCavanaugh
Copy link
Member

Agree that's not the desired behavior, but did you find this writing real code?

@ahejlsberg
Copy link
Member

Your example compiles with no error using the nightly build. The issue was fixed with our improved support for literal types in #10676.

@ahejlsberg ahejlsberg added the Fixed A PR has been merged for this issue label Nov 11, 2016
@Ristaaf
Copy link
Author

Ristaaf commented Nov 16, 2016

RyanCavanaough: This was found when turning on strictNullChecks on an existing codebase, not the example that I showed of course, that was just a simplification to be clear.

We had complex logical expressions with && that relied on returning a string. They could have returned other types too, but we did not handle that, so to understand what was going on and to fix the code, we started to read up on the && operator and what it returns in different circumstances, then we tried those circumstances out in TypeScript and found the error reported here.

@microsoft microsoft locked and limited conversation to collaborators Jun 19, 2018
Sign up for free to subscribe to this conversation on GitHub. Already have an account? Sign in.
Labels
Fixed A PR has been merged for this issue
Projects
None yet
Development

No branches or pull requests

3 participants