Types for async functions do not handle `void` assignments like normal ones
See original GitHub issueTypeScript Version: 3.4.0-dev.201xxxxx
Search Terms:
Code
const w: void = "";
const x: () => void = () => "";
const y: () => Promise<void> = async () => "";
function a(): void {
return "";
}
async function b(): Promise<void> {
return "";
}
const y2: () => Promise<() => void> = async () => () => "";
function a2(): () => void {
return () => "";
}
function b2(): Promise<() => void> {
return () => "";
}
Expected behavior: Generally speaking, all of them should be an error, or all of them shouldn’t be.
Actual behavior:
We have a special “function returning void” rule that allows () => ""
to be assignable to () => void
, but this doesn’t carry thru to async scenarios, so a async () => ""
is not assignable to a () => Promise<void>
.
Issue Analytics
- State:
- Created 4 years ago
- Reactions:6
- Comments:6 (2 by maintainers)
Top Results From Across the Web
Typescript async function return type void vs Promise<void>
Defining a function as async will just wrap the return value in a promise, which the () => void type will just ignore....
Read more >Async/Await - Best Practices in Asynchronous Programming
Async void methods have different composing semantics. Async methods returning Task or Task<T> can be easily composed using await, Task.WhenAny, Task.WhenAll ...
Read more >async function - JavaScript - MDN Web Docs - Mozilla
An async function will return a different reference, whereas Promise.resolve returns the same reference if the given value is a promise. It can...
Read more >Asynchronous Operations: Guidelines
And while it is OK to have more async functions than not—in fact, we should generally not be afraid to make a function...
Read more >Async/await in TypeScript - LogRocket Blog
async /await is essentially a syntactic sugar for promises, which is to say the async/await keyword is a wrapper over promises. An async...
Read more >Top Related Medium Post
No results found
Top Related StackOverflow Question
No results found
Troubleshoot Live Code
Lightrun enables developers to add logs, metrics and snapshots to live code - no restarts or redeploys required.
Start FreeTop Related Reddit Thread
No results found
Top Related Hackernoon Post
No results found
Top Related Tweet
No results found
Top Related Dev.to Post
No results found
Top Related Hashnode Post
No results found
Top GitHub Comments
Tbh, I think
void
just needs to behave likeunknown
except we also have lint-level checks to attempt to forbid real-valued returns in implementations whose expected return position type isvoid
.I don’t know, I think it’s kind of elegant right now that
void
is in a duality withany
. 😃any
: Do whatever you want, I won’t bother you but runtime errors are your problem nowvoid
: I’ll put whatever here, don’t try to stop me (by inspecting the value) or elseWhere “you” = programmer, “I” = type system.