Consider this function:
function foo(check: boolean){
if(check){
return 123;
}
}
Why is the implicit return type only number rather than number | void?
Stackblitz: https://stackblitz.com/edit/typescript-jwy5xn?file=index.ts
>Solution :
The primary issue here seems to be that you are not using the --strictNullChecks compiler option, part of the --strict suite of compiler options that is more or less considered a "standard" set of type safety features and is strongly recommended.
With --strictNullChecks disabled, the values null and undefined are considered assignable to every other type, and get absorbed by them. Any operation that could return, say, number or undefined, will be shown as returning just number. Which is not great.
If you enable --strictNullChecks then you’ll see that the return type of your function is the union number | undefined, which would hopefully meet your needs.
(Note that number | undefined isn’t exactly the same as number | void. But the relationship between void and undefined is somewhat muddy and, I think, not directly related to why you only saw number, which I interpret as the real question. If you want to know more about that you can look at Why is undefined assignable to void? or Why does TypeScript have both `void` and `undefined`? etc.)