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 type inference issue with supertypes #13420

Closed
rkirov opened this issue Jan 11, 2017 · 2 comments
Closed

Promise type inference issue with supertypes #13420

rkirov opened this issue Jan 11, 2017 · 2 comments
Labels
Duplicate An existing issue was already created

Comments

@rkirov
Copy link
Contributor

rkirov commented Jan 11, 2017

TypeScript Version: 2.1.4

Code

declare let p: Promise<number>;
declare let p2: Promise<number|string>;

// TS infers pfinal to be Promise<number> incorrectly.
// pfinal should be Promise<number|string>
let pfinal = p.then(_ => p2);

Expected behavior:
Pfinal should be Promise<number|string>

Actual behavior:
Pfinal is inferred to be Promise<number>

Note any choice of Promise<T> and Promise<S> where S is a supertype of T will trigger this error.

This happens because then function overload then(onfulfilled?: ((value: T) => T | PromiseLike<T>) | undefined | null, ...): Promise<T>; is matching before the one with the free type parameter TResult.

As far I can tell it is a consequence of the fix in #10524 (comment) and not a desired change.

//cc @calebegg

@aluanhaddad
Copy link
Contributor

aluanhaddad commented Jan 12, 2017

This is related to #13015, #13008 and in particular this comment from the latter #13008 (comment)
it seems the overload

then(
    onfulfilled?: ((value: T) => T | PromiseLike<T>) | undefined | null, 
    onrejected?: ((reason: any) => T | PromiseLike<T>) | undefined | null
): Promise<T>;

is not only redundant but is causing this specific bug.

@rkirov
Copy link
Contributor Author

rkirov commented Jan 12, 2017

Agree, thanks for reference, closing in favor of #10977 as the canonical bug for this.

Sign up for free to subscribe to this conversation on GitHub. Already have an account? Sign in.
Labels
Duplicate An existing issue was already created
Projects
None yet
Development

No branches or pull requests

3 participants