Skip to content

Incorrect return types for Promise#catch and 0- and 2- arg forms of Promise#then #9193

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

Closed
jeffreymorlan opened this issue Jun 15, 2016 · 2 comments
Labels
Fixed A PR has been merged for this issue Suggestion An idea for TypeScript

Comments

@jeffreymorlan
Copy link
Contributor

jeffreymorlan commented Jun 15, 2016

Expression (p: Promise) Correct type Current type
p.then() Promise Promise<{}>
p.then(b => 1) Promise Promise
p.then(b => 1, e => 'error') Promise<number string>
p.then(b => 1, e => {}) Promise<number void>
p.then(b => 1, e => {throw Error()}) Promise Promise
p.then(b => 1, e => Promise.reject(Error())) Promise Promise
p.catch(e => 'error') Promise<boolean string>
p.catch(e => {}) Promise<boolean void>
p.catch(e => {throw Error()}) Promise Promise
p.catch(e => Promise.reject(Error())) Promise Promise

Using overloading and the new never type, PromiseLike/Promise could be typed to handle all these cases correctly:

interface PromiseLike<T> {
    then(): PromiseLike<T>;
    then<R1>(onfulfilled: (value: T) => R1 | PromiseLike<R1>): PromiseLike<R1>;
    then<R1, R2>(onfulfilled: (value: T) => R1 | PromiseLike<R1>,
                 onrejected: (reason: any) => R2 | PromiseLike<R2>): PromiseLike<R1 | R2>;
}

interface Promise<T> {
    then(): Promise<T>;
    then<R1>(onfulfilled: (value: T) => R1 | PromiseLike<R1>): Promise<R1>;
    then<R1, R2>(onfulfilled: (value: T) => R1 | PromiseLike<R1>,
                 onrejected: (reason: any) => R2 | PromiseLike<R2>): Promise<R1 | R2>;
    catch<R2>(onrejected: (reason: any) => R2 | PromiseLike<R2>): Promise<T | R2>;
}

declare var Promise: {
    reject(reason: any): Promise<never>; // Returned promise is never fulfilled
};
@mhegazy
Copy link
Contributor

mhegazy commented Jun 15, 2016

//cc: @rbuckton

@rbuckton
Copy link
Member

There are a few things we need to fix in the checker for properly handling never in these cases with async functions, and there's a breaking change in the definition for PromiseLike and Promise that needs to be resolved, but this seems like the right direction.

@mhegazy mhegazy added the Suggestion An idea for TypeScript label Jun 15, 2016
rbuckton added a commit that referenced this issue Jun 16, 2016
… async functions due to introduction of never type.

Fixes #9193.
@mhegazy mhegazy added this to the TypeScript 2.0 milestone Jun 22, 2016
@mhegazy mhegazy added the Fixed A PR has been merged for this issue label Jun 22, 2016
@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
Fixed A PR has been merged for this issue Suggestion An idea for TypeScript
Projects
None yet
Development

No branches or pull requests

3 participants