Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

instanceof typeguard fails with derived classes that have unsound overrides #17612

Closed
SebGrenier opened this issue Aug 4, 2017 · 0 comments · Fixed by #19671
Closed

instanceof typeguard fails with derived classes that have unsound overrides #17612

SebGrenier opened this issue Aug 4, 2017 · 0 comments · Fixed by #19671
Assignees
Labels
Bug A bug in TypeScript Fixed A PR has been merged for this issue

Comments

@SebGrenier
Copy link

TypeScript Version:

2.3.4 and 2.4.1

Code

interface IBaseModel {
    id: string
}

class BaseClass {
    model: IBaseModel
    constructor() {
    }

    setModel(model: IBaseModel) {
        this.model = model
    }

    getValueByName(name: string) {
        return this.model[name];
    }
}

interface IDerived1Model extends IBaseModel {
    height: number;
}

class Derived1 extends BaseClass {
    // Unsound override!
    setModel(model: IDerived1Model) {
        super.setModel(model);
        // Do something with model...
    }
}

interface IDerived2Model extends IBaseModel {
    width: number;
}

class Derived2 extends BaseClass {
    // Unsound override!
    setModel(model: IDerived2Model) {
        super.setModel(model);
        // Do something with model...
    }
}

const model1 = { id: "0", height: 42 };
const model2 = { id: "1", width: 24 };

const obj1 = new Derived1();
obj1.setModel(model1);

const obj2 = new Derived2();
obj2.setModel(model2);

const objs: BaseClass[] = [
    obj1,
    obj2
];

let variable: any = null;
for (const obj of objs) {
    if (obj instanceof Derived1) {
        variable = obj.getValueByName("height"); // Ok, obj is now of type `Derived1`
    } else if (obj instanceof Derived2) {
        variable = obj.getValueByName("width"); // Does not compile: Property 'getValueByName' does not exist on type 'never'
    }
    console.log("Value is: " + variable);
}

Expected behavior:
obj is narrowed to Derived2.

Actual behavior:
obj is narrowed to never.

More

See discussion here: https://stackoverflow.com/questions/45381122/typescript-type-narrowed-to-never-with-instanceof-in-an-if-else-statement
When the setModel overrides use IBaseModel as type, it works. However, even though the current code is unsound, I would expect it to compile.

@DanielRosenwasser DanielRosenwasser added the Bug A bug in TypeScript label Aug 27, 2017
@DanielRosenwasser DanielRosenwasser added this to the TypeScript 2.6 milestone Aug 27, 2017
@mhegazy mhegazy removed this from the TypeScript 2.6 milestone Aug 31, 2017
@mhegazy mhegazy added the Fixed A PR has been merged for this issue label Nov 2, 2017
@mhegazy mhegazy added this to the TypeScript 2.7 milestone Nov 2, 2017
@microsoft microsoft locked and limited conversation to collaborators Jun 14, 2018
Sign up for free to subscribe to this conversation on GitHub. Already have an account? Sign in.
Labels
Bug A bug in TypeScript Fixed A PR has been merged for this issue
Projects
None yet
Development

Successfully merging a pull request may close this issue.

4 participants