Skip to content

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

Closed
@IEvangelist

Description

@IEvangelist

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

Metadata

Metadata

Assignees

No one assigned

    Labels

    Design LimitationConstraints of the existing architecture prevent this from being fixed

    Type

    No type

    Projects

    No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions