question-mark
Stuck on an issue?

Lightrun Answers was designed to reduce the constant googling that comes with debugging 3rd party libraries. It collects links to all the places you might be looking at while hunting down a tough bug.

And, if you’re still stuck at the end, we’re happy to hop on a call to see how we can help out.

Types for async functions do not handle `void` assignments like normal ones

See original GitHub issue

TypeScript 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>.

Playground Link

Issue Analytics

  • State:open
  • Created 4 years ago
  • Reactions:6
  • Comments:6 (2 by maintainers)

github_iconTop GitHub Comments

3reactions
weswighamcommented, Sep 14, 2019

Tbh, I think void just needs to behave like unknown except we also have lint-level checks to attempt to forbid real-valued returns in implementations whose expected return position type is void.

2reactions
fatcerberuscommented, Sep 14, 2019

I don’t know, I think it’s kind of elegant right now that void is in a duality with any. 😃

  • any: Do whatever you want, I won’t bother you but runtime errors are your problem now
  • void: I’ll put whatever here, don’t try to stop me (by inspecting the value) or else

Where “you” = programmer, “I” = type system.

Read more comments on GitHub >

github_iconTop 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 >

github_iconTop Related Medium Post

No results found

github_iconTop Related StackOverflow Question

No results found

github_iconTroubleshoot Live Code

Lightrun enables developers to add logs, metrics and snapshots to live code - no restarts or redeploys required.
Start Free

github_iconTop Related Reddit Thread

No results found

github_iconTop Related Hackernoon Post

No results found

github_iconTop Related Tweet

No results found

github_iconTop Related Dev.to Post

No results found

github_iconTop Related Hashnode Post

No results found