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?
- Prevents dense, hard-to-scan template literals that bury logic inside interpolation.
- Encourages descriptive variable names that explain the branching intent.
- 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.
| Option | Type | Default | Description |
|---|---|---|---|
ignoreFiles | string[] | [] | 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.