Skip to content

Equality operator with unknown does not narrow the type of the unknown #25172

Closed
@MadaraUchiha

Description

@MadaraUchiha

TypeScript Version: 3.0.0-dev.20180623

Search Terms: typescript unknown equality guard

Code

let x: unknown; // reproduces with const too
if (x === 5) {
    let y = x.toString(10);
}

Expected behavior:
x inside of the if block to be of type 5, y to get type of string, everyone happy.
Actual behavior:
x inside of the if block is still of type unknown, I get an error for x being of type unknown and therefore does not have a property toString.
Playground Link: N/A (dev build)

Related Issues: #24439

Metadata

Metadata

Assignees

No one assigned

    Labels

    CommittedThe team has roadmapped this issueHelp WantedYou can do thisSuggestionAn idea for TypeScript

    Type

    No type

    Projects

    No projects

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions