Skip to content

TypeChecker::getTypeAtLocation(Identifier) does not Return Narrowed Type Inside an Array.isArray()-Based Type Guard #8094

Closed
@SlurpTheo

Description

@SlurpTheo

1.8.10 / next (1.9.0-dev.20160414)

Code

export function X() {
    let x: number | number[] = 0;

    if (typeof x === "number") {
        x;              // ts.createProgram(...).getTypeChecker().getTypeAtLocation(Identifier::x).getFlags() --> number
        const y = x;    // ts.createProgram(...).getTypeChecker().getTypeAtLocation(Identifier::y).getFlags() --> number
    }
    if (typeof x !== "object") {
        x;              // ts.createProgram(...).getTypeChecker().getTypeAtLocation(Identifier::x).getFlags() --> number
        const y = x;    // ts.createProgram(...).getTypeChecker().getTypeAtLocation(Identifier::y).getFlags() --> number
    }
    if (!Array.isArray(x)) {
        x;              // ts.createProgram(...).getTypeChecker().getTypeAtLocation(Identifier::x).getFlags() --> Union
        const y = x;    // ts.createProgram(...).getTypeChecker().getTypeAtLocation(Identifier::y).getFlags() --> number
    }

Behavior:
Why does TypeChecker::getTypeAtLocation(Identifier::x) correctly report number after the first (typeof x === "number") & second (typeof x !== "object") type guards, but report the (un-narrowed) Union type after the third (!Array.isArray(x)) type guard? All three block's const c is correctly typed as number. Is there something besides TypeChecker::getTypeAtLocation(Identifier) to use here?

Metadata

Metadata

Assignees

No one assigned

    Labels

    APIRelates to the public API for TypeScriptBugA bug in TypeScript

    Type

    No type

    Projects

    No projects

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions