Surprising `Not all code paths return a value`
See original GitHub issueTypeScript 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:
- Created 6 years ago
- Reactions:12
- Comments:8 (3 by maintainers)
Top 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 >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
Just wanted to point out that this generally works without errors with the “No implicit returns” flag:
Playground link
compilerOptions:{ “noImplicitReturns”: false }