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

Inconsistent type on Thenable.then overload? #347

Open
Lucretiel opened this issue May 13, 2019 · 1 comment
Open

Inconsistent type on Thenable.then overload? #347

Lucretiel opened this issue May 13, 2019 · 1 comment

Comments

@Lucretiel
Copy link
Contributor

Lucretiel commented May 13, 2019

then <U> (onFulfilled?: (value: R) => U | Thenable<U>, onRejected?: (error: any) => void): Thenable<U>;

One of the overloads for Thenable.then is:

then <U> (
    onFulfilled?: (value: R) => U | Thenable<U>,
    onRejected?: (error: any) => void,
): Thenable<U>;

However, consider this example:

let promise = Promise<number>.reject(new Error("error"));

let newPromise = promise.then(
    (value: number): string => value.toString(),
    (error: any): void => { console.log(error); },
);

The type of newPromise, based on the overload, is Promise<string>. However, because the onRejected function doesn't throw or return a rejected promise, newPromise becomes a resolved promise with the return value of onRejected, which is void, which means that newPromise should be Promise<T | void>. Specifically, this code should not compile:

newPromise.then((value: string) => {
    // value should be `(string | void)`, since it could have a
    //value from the `onRejected` branch
});
@Lucretiel
Copy link
Contributor Author

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

No branches or pull requests

1 participant