Closed
Description
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.
Metadata
Metadata
Assignees
Type
Projects
Milestone
Relationships
Development
No branches or pull requests
Activity
RyanCavanaugh commentedon Nov 11, 2016
Agree that's not the desired behavior, but did you find this writing real code?
ahejlsberg commentedon Nov 11, 2016
Your example compiles with no error using the nightly build. The issue was fixed with our improved support for literal types in #10676.
Ristaaf commentedon 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.