Incorrect TS18047 Error --> "variable is possibly null" · Issue #63426 · microsoft/TypeScript · GitHub
Skip to content

Incorrect TS18047 Error --> "variable is possibly null" #63426

@PoseidonEnergy

Description

@PoseidonEnergy

🔎 Search Terms

TypeScript incorrectly thinks the selection constant in the following code could be null:

Playground link

const selection = window.getSelection();

if (selection != null) {
    function clearSelection() {
        selection.empty(); // <-- TypeScript incorrectly says selection could be null here
    }

    clearSelection();
}
Image

🕗 Version & Regression Information

v6.0.3

🙁 Actual behavior

A TS18047 error is displayed.

🙂 Expected behavior

No errors should be displayed.

Metadata

Metadata

Assignees

No one assigned

    Labels

    DuplicateAn existing issue was already created

    Type

    No type

    Projects

    No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions