-
Notifications
You must be signed in to change notification settings - Fork 12.8k
Promise<non-void> not assignable to Promise<void> w/fn return type #49755
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
Comments
Duplicate of #12871 ? |
Possibly, the way some of the comments on that have leaned. Solving that one would address Set 3 above, and therefore also address this one. However, it seems like it could also be possible to address set 2 without addressing set 3 by creating an assignability rule that only works when it's a function return type, e.g. |
Current workaround: use |
This caught me off guard.
|
Tried implementing a simple write stream: interface OutputStream<T> {
/** resolve when data is written successfully, reject if failed to write */
write(data: T): Promise<void>;
} Now I want my HTTP client to implement this: class HttpClient implements OutputStream<HttpRequest> {
write(data: HttpRequest): Promise<HttpResponse>;
} Oops, not possible. Even though it should work fine when used: const http = new HttpClient();
const res = await http.write(...); // HttpResponse
const ws: OutputStream<HttpRequest> = http; // hide object behind interface
await ws.write(...); // void return type |
Bug Report
🔎 Search Terms
promise void assignable 2345
🕗 Version & Regression Information
⏯ Playground Link
Playground link with relevant code
💻 Code
🙁 Actual behavior
Errors as noted in code comments.
🙂 Expected behavior
Async functions that resolve non-void are assignable to async function types that resolve void, just like synchronous functions that return non-void are assignable to synchronous function types that return void.
The string parameter is given in the example code to maintain the idea that the function may have desirable side effects and thus be worth calling, even if the resolve value isn't relevant to the calling context.
The FAQ states (with only example function names changed) that
Why isn't the same true of async functions?
It's pretty annoying to have to write a wrapper function around every async function just to have a void-resolving function that the TypeScript compiler demands, as another example of valid JavaScript that has to be rewritten with more than just type annotations in a conversion.
The text was updated successfully, but these errors were encountered: