TypeScript: Chained promises produce incorrect types

TypeScript Version: nightly (2.1.0-dev.20160918)

Code

class A {
    json(): Promise<any> {
        return Promise.resolve({});
    };
}

function test() {
    let p = Promise.resolve(new A());
    p.then(x => x.json()).then(x => x.b);
}

Expected behavior: Compiled without errors

Actual behavior: Compiled with an error

error TS2339: Property 'b' does not exist on type 'A'.

About this issue

  • Original URL
  • State: closed
  • Created 8 years ago
  • Reactions: 13
  • Comments: 20 (4 by maintainers)

Commits related to this issue

Most upvoted comments

The changes made to the definition of Promise were added to address issues with assignability (#10524) and strict null checks. Just changing the order of the overloads does not solve the problem, as some ordering works better with strict null checks turned on, but makes things worse with strict null checks turned off.

I am currently investigating a solution that seems to be giving the right answer in all of these cases, but it needs further testing and discussion before I can put up a PR.

This is huge. Could we get an official statement? This is preventing us from upgrading to 2.1 because all of our Promise chains fail to compile.

There is a difference between can and must. This is clearly a serious regression

On 28 Dec 2016 3:12 AM, “Alexander Ekdahl” notifications@github.com wrote:

You can be explicit and use then which let’s you specify the result of your then block.

— You are receiving this because you authored the thread. Reply to this email directly, view it on GitHub https://github.com/Microsoft/TypeScript/issues/10977#issuecomment-269346150, or mute the thread https://github.com/notifications/unsubscribe-auth/ADimvNfzi4-xpLjxtJYeH8C8SvvbqN26ks5rMTj2gaJpZM4KAGp9 .

You can be explicit and use then<T> which let’s you specify the result of your then block.

I confirm, this issue isn’t fixed in latest typescript 2.2.1 This is a huge blocker !