Skip to content

Polymorphic this type not inferred as expected in super calls #7728

Description

TypeScript Version:

1.8.7

Code

class Base {

    public itemFactory: (i: any) => this;

    public save() {
        var xhr: PromiseLike<any> = fetch(....);
        return xhr.then((value) => this.itemFactory(value)); // becomes PromiseLike<this> here
    }
}

class Extend extends Base {
    public inExtended = true;

    public save() {
        // doing some stuff before calling save
        return super.save();
    }
}


var e = new Extend();
e.save().then(value => value.inExtended); // Compile Error. `inExtended` not in Base
e.itemFactory(new Object()).inExtended = true; // No error

// yet, if I don't override `save` the types are inferred as expected
class Extend2 extends Base {
    public inExtended2 = true;
}

var e2 = new Extend2();
e2.save().then(value => value.inExtended2); // No error
e2.itemFactory(new Object()).inExtended2 = true; // No error

Expected behavior:
I expected Extends.save have the type () => PromiseLike<this>.
Actual behavior:
It instead it infers () => PromiseLike<Base>.

A pretty simple workaround for me is to add another PromiseLike<this> return type annotation, but I really expected the PromiseLike<this> to carry through. Especially since if I don't override save like in Extend2, the this type becomes Extend2.

Is it possible to have the type system carry the this type along so Extend.save will automatically infer the return type PromiseLike<this>?

Activity

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Metadata

Metadata

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