close

restrict-template-expressions

Added in v0.1.4

Configuration

PresetConfigured Value
✅ ts.configs.recommendedTypeChecked"error"
✅ ts.configs.strictTypeChecked["error",{"allowAny":false,"allowBoolean":false,"allowNever":false,"allowNullish":false,"allowNumber":false,"allowRegExp":false}]
rslint.config.ts
import { defineConfig, ts } from '@rslint/core';

export default defineConfig([
  ts.configs.recommended,
  {
    rules: {
      '@typescript-eslint/restrict-template-expressions': 'error',
    },
  },
]);

Rule Details

Enforce template literal expressions to be of string type. When a value is interpolated into a template literal (${expr}), it is implicitly converted to a string, which produces results such as "[object Object]" for plain objects. This rule restricts which types may be interpolated.

By default, primitives that stringify predictably are permitted (number, bigint, boolean, null, undefined, any, RegExp), along with Error, URL, and URLSearchParams and their subclasses.

Examples of incorrect code for this rule:

declare const obj: object;
const msg = `result: ${obj}`;

declare const arr: string[];
const msg2 = `items: ${arr}`;

declare const sym: symbol;
const msg3 = `symbol: ${sym}`;

Examples of correct code for this rule:

const name = 'world';
const greeting = `Hello, ${name}`;

declare const obj: object;
const msg = `result: ${JSON.stringify(obj)}`;

declare const arr: string[];
const msg2 = `items: ${arr.join(', ')}`;

Options

OptionTypeDefaultDescription
allow(string | TypeOrValueSpecifier)[][{ from: 'lib', name: ['Error', 'URL', 'URLSearchParams'] }]Additional types to permit, matched against the type or any of its base types.
allowAnybooleantruePermit any typed values.
allowArraybooleanfalsePermit arrays and tuples whose element type is itself permitted.
allowBooleanbooleantruePermit boolean typed values.
allowNeverbooleanfalsePermit never typed values.
allowNullishbooleantruePermit null and undefined.
allowNumberbooleantruePermit number and bigint typed values.
allowRegExpbooleantruePermit RegExp typed values.

To require every interpolated value to be a string, empty the allow list and turn each allow* option off:

{
  "@typescript-eslint/restrict-template-expressions": [
    "error",
    {
      "allow": [],
      "allowAny": false,
      "allowBoolean": false,
      "allowNever": false,
      "allowNullish": false,
      "allowNumber": false,
      "allowRegExp": false
    }
  ]
}

Original Documentation