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

Chained promises produce incorrect types #10977

Closed
MaximBalaganskiy opened this issue Sep 19, 2016 · 20 comments · Fixed by #13487
Closed

Chained promises produce incorrect types #10977

MaximBalaganskiy opened this issue Sep 19, 2016 · 20 comments · Fixed by #13487
Assignees
Labels
Bug A bug in TypeScript Fixed A PR has been merged for this issue

Comments

@MaximBalaganskiy
Copy link

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'.
@MaximBalaganskiy MaximBalaganskiy changed the title Chained promised cause compilation errors Chained promised produce incorrect types Sep 19, 2016
@MaximBalaganskiy MaximBalaganskiy changed the title Chained promised produce incorrect types Chained promises produce incorrect types Sep 19, 2016
@yortus
Copy link
Contributor

yortus commented Sep 19, 2016

I can repro this. This made it a bit clearer:

function test() {
    let p1 = Promise.resolve(new A());  // p1 is Promise<A>
    let p2 = p1.then(x => x.json());    // p2 is Promise<A>, but should be promise<any>
    let p3 = p2.then(x => x.b);         // x is A, but should be any
}

The incorrect inference ocurrs on the let p2... line and is a result of choosing the first then overload from the Promise<T> definition in lib.d.ts.

It looks like a result of the changes to the then overloads made recently in #10448.

@wizzard0
Copy link

Also, Promise.all is still inferred incorrectly sometimes.

@blakeembrey
Copy link
Contributor

blakeembrey commented Sep 28, 2016

Just updated from 2.1.0-dev.20160911 to 2.1.0-dev.20160928 and run into this. Something simple like Promise.resolve({}).then(() => 'test') now keeps the return type of Promise<{}> instead of Promise<string>.

@doxiaodong
Copy link

same with 2.1.1,

@jwbay
Copy link
Contributor

jwbay commented Dec 2, 2016

Another problem I hit and a non-obvious workaround (shortened from RWC) --

function test2() {
    return new Promise<number>(resolve => {
        fetch('').then(response => {
            //without the cast, value below is Response and resolve() errors
            //in TS 2.0 value is any since json() returns Promise<any>
            return response.json() as Promise<number>; 
        }).then(value => {
            resolve(value);
        });
    });
}

@Offirmo
Copy link

Offirmo commented Dec 7, 2016

This is a major bug !

@Iezious
Copy link

Iezious commented Dec 8, 2016

same with 2.1.4, moved to async calls since this bug

@Alexander-Taran
Copy link

2.0.10 got the same behavior, Had to switch back to 2.0.7

@doxiaodong
Copy link

@Alexander-Taran, 2.0.10 is not same as 2.1.x in my code.

@mhegazy mhegazy added the Bug A bug in TypeScript label Dec 16, 2016
@mhegazy mhegazy added this to the TypeScript 2.2 milestone Dec 16, 2016
@AlexanderEkdahl
Copy link

AlexanderEkdahl commented Dec 27, 2016

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

@MaximBalaganskiy
Copy link
Author

MaximBalaganskiy commented Dec 27, 2016 via email

@raijinsetsu
Copy link

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.

@rbuckton
Copy link
Member

rbuckton commented Jan 6, 2017

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.

@mhegazy mhegazy added the Fixed A PR has been merged for this issue label Feb 15, 2017
@FriOne
Copy link

FriOne commented Feb 25, 2017

@mhegazy this isn't fixed for me. I use https:/shlomiassaf/angular2-modal and this module have types problem from above 2.0.10.

let modal = this.modal.open(AddFolderDialogComponent, {context: new AddFolderContext(parentFolder)});
    modal
      .then(dialog => dialog.result)
      .then((folder: Folder) => this.store.dispatch(new AddFolderAction(folder)))

open() returns Promise<DialogRef<any>>, DialogRef.result contains Promise<any>, I get an error:

TS2345: Argument of type '(folder: Folder) => void' is not assignable to parameter of type '(value: DialogRef) => void | PromiseLike'.
Types of parameters 'folder' and 'value' are incompatible.
Type 'DialogRef' is not assignable to type 'Folder'.
Property 'id' is missing in type 'DialogRef'.

If I'm wrong, please, show me a way to fix it.

@Offirmo
Copy link

Offirmo commented Feb 27, 2017

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

@MaximBalaganskiy
Copy link
Author

MaximBalaganskiy commented Feb 27, 2017 via email

@FriOne
Copy link

FriOne commented Feb 27, 2017

@MaximBalaganskiy Hm, this is a huge blocker as @Offirmo sad. I can't update typescript and angular because it depends on 2.1 now. Also 2.1 has object spreading :( @mhegazy Why it was moved to 2.3?

@Offirmo
Copy link

Offirmo commented Feb 28, 2017

@MaximBalaganskiy 2.3 isn't available in npm yet. Can't wait...

@MaximBalaganskiy
Copy link
Author

npm install typescript@next -g should install the latest

@FriOne
Copy link

FriOne commented Feb 28, 2017

@MaximBalaganskiy Sorry I understood you incorrectly. I thougth 2.3 is unavailable yet.

CjS77 pushed a commit to coinbase/coinbase-pro-trading-toolkit that referenced this issue Mar 13, 2018
According to microsoft/TypeScript#10977 the
bug was fixed in TS2.3.
@microsoft microsoft locked and limited conversation to collaborators Jun 19, 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.