Open
Description
Suggestion
π Search Terms
return never tagged template unreachable code
β Viability Checklist
My suggestion meets these guidelines:
- This wouldn't be a breaking change in existing TypeScript/JavaScript code
- This wouldn't change the runtime behavior of existing JavaScript code
- This could be implemented without emitting different JS based on the types of the expressions
- This isn't a runtime feature (e.g. library functionality, non-ECMAScript syntax with JavaScript output, new syntax sugar for JS, etc.)
- This feature would agree with the rest of TypeScript's Design Goals.
β Suggestion
Control flow analysis detecting unreachable code after a tagged template function call that throws.
π Motivating Example
It's a surprise that the same function call does not have control flow analysis when used as a tagged template:
function throws(opt?: any): never {
throw new Error()
}
function testFn() {
throws(`reason`);
// @ts-expect-error unreachable
console.log("is unreachable!")
}
function testTag() {
throws`reason`;
// ts does not detect that this is unreachable
console.log("is unreachable!")
}
π» Use Cases
Throwing an exception with a message
Normally this would be something like,
assert(condition, 'explanation');
However for performance reasons we eschew the function call:
condition || Fail`explanation`;
The asserts
works but the Fail
doesn't. We could work around this by Fail('explanation')
but we prefer to be consistent in use of the Fail
function. So for our current work-around is to explicitly throw
the tagged template.