Closed
Description
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).