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.

Surprising `Not all code paths return a value`

See original GitHub issue

TypeScript Version: 2.5.2

Code

type AsyncFunction<T> = () => Promise<T>;

const f: AsyncFunction<number | void> = async () => {
  if (Math.random()) {
    return 1;
  }
};

Expected behavior:

No tsc errors.

Actual behavior:

const f: AsyncFunction<number | void> = async () => {
//                                      ~~~~~~~~~~~~~ [ts] Not all code paths return a value.

  if (Math.random()) {
    return 1;
  }
};

Notably, when using return-type annotation, this error does not occur:

async function a(): Promise<number | void> {
  if (Math.random()) {
    return 1;
  }
}

const b = async (): Promise<number | void> => {
  if (Math.random()) {
    return 1;
  }
};

In our real-world scenario, it would not be sufficient to look for this syntactic pattern. Rather, the fact that the async function is assigned to a union containing void should be the trigger that silences this error.

Issue Analytics

  • State:open
  • Created 6 years ago
  • Reactions:12
  • Comments:8 (3 by maintainers)

github_iconTop GitHub Comments

4reactions
chancancodecommented, Sep 13, 2017

Just wanted to point out that this generally works without errors with the “No implicit returns” flag:

class Foo {
  maybeReturn(): string | void {
    if (Math.random() > 0.5) {
      return 'hello';
    }
  }
}

Playground link

3reactions
rohitkhurmi095commented, Mar 20, 2021

compilerOptions:{ “noImplicitReturns”: false }

Read more comments on GitHub >

github_iconTop Results From Across the Web

C# compiler error: "not all code paths return a value"
When the compiler looks at your code, it's sees a third path (the else you didn't code for) that could occur but doesn't...
Read more >
Not All Code Paths Return a Value: How To Fix This Issue
When you run a set of instructions on your program and it displays not all code paths return a value, it's an error...
Read more >
Surprising Not all code paths return a value #18319 - GitHub
TypeScript Version: 2.5.2 Code type AsyncFunction = () => Promise ; const f: ... Surprising Not all code paths return a value #18319....
Read more >
Solve TypeScript 'Not all code paths return a value' by ...
Yes, if we are not supposed to return anything if there is no match, why do we have to return null? Can't we...
Read more >
Not all Code Paths return a Value (C#) : r/learnprogramming
I think the compiler is being stupid about the not all code paths returning a value. To make it happy, return something at...
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