I'm grappling with a typescript issue - I have a custom function that consistently throws an error, which is used to handle null variables. Strangely, if I directly throw an error without using the function, typescript recognizes that the variable cannot be null afterward. However, when I use a 'never' return type for a function, it does not.
Check out this TS Playground example for a clear demonstration of the problem.
Is there a workaround for this issue?
Currently, my options are limited to:
- Compromising and allowing null in my types (which has significant drawbacks)
- Adding a redundant mock throw statement as shown in the code example (tedious but effective)
I've attempted following the recommended function signature with a return type of never, yet typescript still treats it differently from an explicit throw statement. My expectation was that if a function never returns, typescript would understand that the remaining code won't execute.