Here is the scenario I'm dealing with (utilizing strictNullChecks):
function neverReturns(): never {
throw new Error();
}
const maybeString: string | null = Math.random() > 0.5 ? "hi" : null;
if (!maybeString) {
neverReturns();
// throw new Error();
}
maybeString.substr(0, 1); // <- Object is possibly 'null'.
When throwing an Error directly in the condition, the compiler understands that maybeString cannot be null afterwards and approves the code.
I would anticipate the same outcome when invoking a function with a return type of never
. However, the compiler raises concerns that maybeString might be null
.
Could there be a rationale for this behavior or is it a missing functionality in TypeScript?