Skip to main content

No Ternary in Template Literal

Keep template literals readable by disallowing ternary expressions inside interpolation slots.

Summary

The no-ternary-in-template-literal rule reports any ConditionalExpression (ternary) found inside a template literal's ${} interpolation.

It is recommended to extract the ternary into a named variable before embedding it in the template.

Why Use This Rule?

  1. Prevents dense, hard-to-scan template literals that bury logic inside interpolation.
  2. Encourages descriptive variable names that explain the branching intent.
  3. Makes debugging easier because the intermediate value is visible in a debugger.

Examples

Configuration

Options

warning

ignoreFiles is an escape hatch for files where this rule genuinely does not apply. It is not intended for routine use.

OptionTypeDefaultDescription
ignoreFilesstring[][]File patterns to skip.

Autofix

Autofix is not available for this rule. Flagged code must be updated manually.

Troubleshooting

  • Want to keep the ternary inline for very short expressions — It is recommended to extract even short ternaries. The variable name documents the branching intent and the template stays clean.
  • Nested template literals are not flagged — The rule only checks the direct expressions inside ${}. A ternary inside a nested template literal within an expression would be flagged separately.