Skip to content

support control flow analysis of tagged template callsΒ #51426

Open
@turadg

Description

@turadg

Suggestion

πŸ” Search Terms

return never tagged template unreachable code

Related issues:
#32695
#50363

βœ… 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!")
}

(playground)

πŸ’» 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.

Metadata

Metadata

Assignees

No one assigned

    Labels

    Awaiting More FeedbackThis means we'd like to hear from more people who would be helped by this featureSuggestionAn idea for TypeScript

    Type

    No type

    Projects

    No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions