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

Nested promise types (e.g., Promise<Promise<number>>) should not be allowed in TypeScript #43522

Closed
noseratio opened this issue Apr 3, 2021 · 2 comments

Comments

@noseratio
Copy link

Bug Report

πŸ”Ž Search Terms

Nested promises, promise of promise

πŸ•— Version & Regression Information

  • This is the behavior in every version I tried, and I reviewed the FAQ for entries about nested promises

⏯ Playground Link

Playground link with relevant code

πŸ’» Code

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

async function f2(): Promise<number> { 
    return 42; 
}

async function f3(): Promise<number> { 
    return Promise.resolve(42); 
}

async function f4(): Promise<Promise<number>> { 
    const p = Promise.resolve(42);
    return Promise.resolve(p); 
}

void async function main() {
  console.log(await f1());
  console.log(await f2());
  console.log(await f3());
  console.log(await f4());
} ().catch(e => console.error(e));

πŸ™ Actual behavior

This code compiles without an error or warning. Because JavaScript promises get automatically unwrapped, f1, f2, f3 and f4 have essentially the same behavior to the caller, they all return a promise that resolves to a number, which is inconsistent with the return type of f4, Promise<Promise<number>>.

πŸ™‚ Expected behavior

There should be an error or a warning for f4, suggesting the type should be Promise<number>. FWIW, it is impossible to have a promise that resolves to another promise in JavaScript, so nested Promise<Promise<number>> doesn't make sense and can be misleading.

For example, a person coming from C# (where nested tasks like Task<Task<int>> is a legit concept), might be tempted to do this in TypeScript:

const outerPromise: Promise<Promise<number>> = f4();
const innerPromise = await outerPromise;
const result = await innerPromise; 

This code compiles, but doesn't behave as one may expect. The inferred type for innerPromise is actually number, not Promise<number>.

@MartinJohns
Copy link
Contributor

Duplicate of #27711.

@noseratio
Copy link
Author

I overlooked the #27711, thanks for pointing out.

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

2 participants