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.resolve fails to infer the correct type when the argument is a union type like T | Promise<T> | PromiseLike<T> #48195

Closed
nahcikuy opened this issue Mar 10, 2022 · 1 comment Β· Fixed by #58213
Labels
Experience Enhancement Noncontroversial enhancements Suggestion An idea for TypeScript
Milestone

Comments

@nahcikuy
Copy link

Bug Report

πŸ”Ž Search Terms

  • Promise.resolve
  • PromiseLike
  • Union Type

πŸ•— Version & Regression Information

  • This changed between versions 3.5.1 and 3.6.2

⏯ Playground Link

Playground link with relevant code

πŸ’» Code

function f1(x: number): number | Promise<number> {
  return Promise.resolve(x);
}

function f2(x: number): number | PromiseLike<number> {
  return Promise.resolve(x);
}

function f3(x: number): number | Promise<number> | PromiseLike<number> {
  return Promise.resolve(x);
}

const g1: Promise<number> = Promise.resolve(f1(42)); //OK
const g2: Promise<number> = Promise.resolve(f2(42)); //OK
const g3: Promise<number> = Promise.resolve(f3(42)); //Fails

πŸ™ Actual behavior

The variable g3 fails the type check with the following error

Type 'Promise<number | Promise<number>>' is not assignable to type 'Promise<number>'.
Type 'number | Promise<number>' is not assignable to type 'number'.
Type 'Promise<number>' is not assignable to type 'number'.

Here, g3 is infered to the type Promise<number | Promise<number>>. However, comparing to the behavior on g1 and g2, this seems to be a bug, which should be Promise<number> instead.

πŸ™‚ Expected behavior

No error.

@weswigham
Copy link
Member

#37615

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
Experience Enhancement Noncontroversial enhancements Suggestion An idea for TypeScript
Projects
None yet
Development

Successfully merging a pull request may close this issue.

3 participants