Skip to content

Type guards have strange narrowing behavior when other guard branches exhaust union constituents #4029

Description

@zpdDG4gta8XKpMCd
module A {
    export const enum Brand {} 
}
interface A {
    brand: A.Brand;
    a: string;
}
function isA(value: S): value is A {
    return 'a' in <any> value;
}

module B {
    export const enum Brand {}
}
interface B {
    brand: B.Brand;
    b: number;
}
function isB(value: S): value is B {
    return 'b' in <any> value;
}

module C {
    export const enum Brand {}
}
function isC(value: S): value is C {
    return isA(value) && isB(value);
}
interface C {
    brand: C.Brand;
    a: string;
    b: number;  
}
type S = A | B | C;

function test(state: S) {
    if (isA(state)) {
            // works: state is A
    } else if (isB(state)) {
            // works: state is B
    } else if (isC(state)) {
            // works: state is C
    } else {
            // hey, state is A again! how come?
    }
}

Metadata

Metadata

Assignees

Labels

BugA bug in TypeScriptFixedA PR has been merged for this issue

Type

No type
No fields configured for issues without a type.

Projects

No projects

Relationships

None yet

Development

No branches or pull requests

Issue actions