TypeScript icon indicating copy to clipboard operation
TypeScript copied to clipboard

Isolated declarations uses type information to emit return type for async function

Open lucacasonato opened this issue 1 year ago • 2 comments

🔎 Search Terms

  • isolated declarations
  • return type
  • async function

🕗 Version & Regression Information

5.5.0-dev.20240514

⏯ Playground Link

https://www.typescriptlang.org/play/?target=99&isolatedDeclarations=true&ts=5.5.0-dev.20240514#code/KYDwDg9gTgLgBAQwM4E8B2BjOAzArpmASwjThAAoBKOAbwCg44pgZcpSaBfRJOABSgQAtoSTAAPADcIhACYA+OpyA

💻 Code

export async function x() {
  return {} as Promise<void>
}

🙁 Actual behavior

TypeScript emits the following:

export declare function x(): Promise<void>;

🙂 Expected behavior

Error, or emit:

export declare function x(): Promise<Promise<void>>;

Without using a type checker, one can not collapse the Promise<Promise<T>> into Promise<T>. This means that with ID, a tool could not always emit identical token lists to TSC, even when ID is enabled.

Additional information about the issue

cc @dragomirtitian

lucacasonato avatar May 16 '24 11:05 lucacasonato

Without using a type checker, one can not collapse the Promise<Promise<T>> into Promise<T>.

Can't you though? It's impossible to observe nested promises at runtime, so it's always safe to collapse them.

fatcerberus avatar May 16 '24 14:05 fatcerberus

This is related to #58334. Neither async or generator functions should allow inferred types in isolated declaration

dragomirtitian avatar May 16 '24 15:05 dragomirtitian