Skip to content

control flow with instanceof not works properly #8383

Closed
@emilgpa

Description

@emilgpa

TypeScript Version:

nightly (1.9.0-dev.20160429)

Code

class AAA {
    fieldA: string;
}

class BBB {
    fieldB: string;
}

class AA {
    data: AAA;
}

class BB {
    data: BBB;
}

class A {
    d: AA;
}

class B {
    d: BB;
}

function main(x: A | B) {
    for (let i of [1, 1]) {
        if (x instanceof A) {
            x.d.data.fieldA;
        } else if (x instanceof B) {
            x.d.data.fieldB; // Not compiles here. 
        }
    }
}

Expected behavior:

To access to the fieldB field of BBB class inside of block } else if (x instanceof B) {

Actual behavior:

The compiler raises the follow error: TS2339: Property 'fieldB' does not exist on type 'AAA'..
The behavior in the version 1.8.10 is correct.

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