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>?
TypeScript Version:
1.8.7
Code
Expected behavior:
I expected
Extends.savehave 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 thePromiseLike<this>to carry through. Especially since if I don't overridesavelike in Extend2, thethistype becomesExtend2.Is it possible to have the type system carry the
thistype along soExtend.savewill automatically infer the return typePromiseLike<this>?