Skip to content

Inline assignment in if statement breaks instanceof #31292

Closed
@cmerighi

Description

@cmerighi

TypeScript Version: 3.4.5

Search Terms:
instanceof

Code

// this works
if (target instanceof DatasourceCommandWidgetElement) {
    this._commandBased = true;
    this._commandElement = target.commandElement;
} else {
    this._commandBased = false;
}

// this fails
if (this._commandBased = (target instanceof DatasourceCommandWidgetElement)) {
    this._commandElement = target.commandElement;
}

Expected behavior:
Type inference respected.

Actual behavior:
Type inference missed.

image

Related Issues:
Might be related to 31291 but, in this case, the if statement directly wraps the type-inferenced instruction. Could be that the assigned variable (kind-of-)takes control and the cast gets lost...

Metadata

Metadata

Assignees

No one assigned

    Labels

    BugA bug in TypeScriptDomain: Control FlowThe issue relates to control flow analysis

    Type

    No type

    Projects

    No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions