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

Promise of type { } ignored when nested #13008

Closed
NoelAbrahams opened this issue Dec 18, 2016 · 3 comments · Fixed by #13487
Closed

Promise of type { } ignored when nested #13008

NoelAbrahams opened this issue Dec 18, 2016 · 3 comments · Fixed by #13487
Assignees
Labels
Bug A bug in TypeScript Fixed A PR has been merged for this issue

Comments

@NoelAbrahams
Copy link

TypeScript Version: 2.1.4

Code

function getDate(): Promise<Date> {
    return Promise.resolve(new Date());
}

function getFoo(): Promise<{}> {
    return Promise.resolve({});
}

function doFoo() : Promise<number> {

    // Error: Type 'Promise<Date>' is not assignable to type 'Promise<number>'.
    return (
        getDate()
            .then(() => {

                return (
                    getFoo()
                        .then(() => {

                            return 10;
                        })
                );
            })
    );
}

Expected behavior:

No compilation error

Actual behavior:

Compiler error: Type 'Promise<Date>' is not assignable to type 'Promise<number>'.

Futher Notes
Changing getFoo to return any other value, e.g. function getFoo(): Promise<void> fixes the compilation error.

@aluanhaddad
Copy link
Contributor

aluanhaddad commented Dec 18, 2016

It looks like the order of the overloads of .then in lib.es2015.promise.d.ts are wrong.
I could be wrong but my understanding is that they need to be declared in decreasing order of specificity. When I change this, it fixes the error.
Here is a more minimal repro (note that no error is reported but that the type of result is wrong).

function getFoo(): Promise<{}> {
    return Promise.resolve({});
}

function doFoo(): Promise<number> {
    const result = getFoo()
        .then(() => {

            return 10;
        });
    return result; // Result has type Promise<{}> should be Promise<number>
}

@NoelAbrahams
Copy link
Author

NoelAbrahams commented Dec 18, 2016

@aluanhaddad, I might even suspect that the non-generic overload of then is redundant and should be removed.

@aluanhaddad
Copy link
Contributor

@NoelAbrahams
Yes I think you are right. Regardless the overload order should be fixed because it is always going to pick the wrong one for cases where T is assignable to TResult as is the case when TResult is {} for any T.

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