Skip to content

Narrowing is not invalidated when side effects occur outside of current scope #19209

@bgnx

Description

@bgnx

Why is there an error?

TypeScript Version: 2.6.0-dev.20171015

Code

enum SomeEnum {
    k1 = 1,
    k2 = 2,
    k3 = 3
}

class SomeClass {
    state: SomeEnum = SomeEnum.k1
    method() {
        if (this.state === SomeEnum.k1) {
            maybeUpdate();
            //error - [ts] Operator '===' cannot be applied to types 'SomeEnum.k1' and 'SomeEnum.k2'.
            if (this.state === SomeEnum.k2) {
                this.state = SomeEnum.k3
            }
        }
    }
}

const obj = new SomeClass();
obj.method();
function maybeUpdate() {
    if (Math.random() < 0.5) {
        obj.state = SomeEnum.k2;
    }
}

Expected behavior:
No error
Actual behavior:
wtf error

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