-
Notifications
You must be signed in to change notification settings - Fork 12.8k
support control flow analysis of tagged template calls #51426
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. Weβll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Comments
This is actually two separate issues, one of which is by design. The pattern
|
Thanks for pointing that out. Is there any issue to track a feature request for Do |
A |
The lack of narrowing based on logical expression was previously filed as issue #50739. We can focus this issue on the lack of control flow analysis through tagged template literal as the following code would show: function throws(opt?: any): never {
throw new Error()
}
declare const a: string | null;
function testFn() {
if (!a) throws(`reason`);
a.charAt(0); // a is correctly narrowed to `string`
}
function testTag() {
if (!a) throws`reason`;
a.charAt(0); // Object is possibly 'null'. ts(2531)
}
function testFnInSwitch() {
switch(a) {
case null: // No fallthrough correctly infered
throws(`reason`);
default:
a.charAt(0); // a is correctly narrowed to `string`
}
}
function testTagInSwitch() {
switch(a) {
case null: // Fallthrough case in switch. ts(7029)
throws`reason`;
default:
a.charAt(0); // Object is possibly 'null'. ts(2531)
}
} Playground Link: Provided |
I have implemented this in 71@de96190, but can't submit a PR as this issue is not in the "Backlog" milestone. Is there any way this could be added to the backlog? The proposed change is fairly minimal, but fixes this issue. |
Gentle ping (sorry) -- would love to submit a PR for this. |
Suggestion
π Search Terms
return never tagged template unreachable code
Related issues:
#32695
#50363
β Viability Checklist
My suggestion meets these guidelines:
β 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:
(playground)
π» Use Cases
Throwing an exception with a message
Normally this would be something like,
However for performance reasons we eschew the function call:
The
asserts
works but theFail
doesn't. We could work around this byFail('explanation')
but we prefer to be consistent in use of theFail
function. So for our current work-around is to explicitlythrow
the tagged template.The text was updated successfully, but these errors were encountered: