Skip to content

Bizarre type guard issue in 2.0 #10378

Closed
Closed
@jeffreymorlan

Description

@jeffreymorlan
var a: boolean | number | string;
if (typeof a !== 'boolean') {
    // a is narrowed to "number | string"
    for (var i = 0; i < 1; i++) {
        for (var j = 0; j < 1; j++) {}
        if (typeof a === 'string') {
            // a is narrowed to "string'
            for (var j = 0; j < 1; j++) {
                a.length; // Error: Property 'length' does not exist on type 'never'.
            }
        }
    }
}

Removing any of the three loops, or the outer type guard, makes the problem disappear.

Metadata

Metadata

Assignees

Labels

BugA bug in TypeScriptFixedA PR has been merged for this issue

Type

No type

Projects

No projects

Relationships

None yet

Development

No branches or pull requests

Issue actions