description |
---|
Enforce template literal expressions to be of `string` type. |
🛑 This file is source code, not the primary documentation location! 🛑
See https://typescript-eslint.io/rules/restrict-template-expressions for documentation.
JavaScript will call toString()
on an object when it is converted to a string, such as when +
adding to a string or in ${}
template literals.
The default Object .toString()
returns "[object Object]"
, which is often not what was intended.
This rule reports on values used in a template literal string that aren't primitives and don't define a more useful .toString()
method.
const arg1 = [1, 2];
const msg1 = `arg1 = ${arg1}`;
const arg2 = { name: 'Foo' };
const msg2 = `arg2 = ${arg2 || null}`;
const arg = 'foo';
const msg1 = `arg = ${arg}`;
const msg2 = `arg = ${arg || 'default'}`;
const stringWithKindProp: string & { _kind?: 'MyString' } = 'foo';
const msg3 = `stringWithKindProp = ${stringWithKindProp}`;
Examples of additional correct code for this rule with { allowNumber: true }
:
const arg = 123;
const msg1 = `arg = ${arg}`;
const msg2 = `arg = ${arg || 'zero'}`;
Examples of additional correct code for this rule with { allowBoolean: true }
:
const arg = true;
const msg1 = `arg = ${arg}`;
const msg2 = `arg = ${arg || 'not truthy'}`;
Examples of additional correct code for this rule with { allowAny: true }
:
const user = JSON.parse('{ "name": "foo" }');
const msg1 = `arg = ${user.name}`;
const msg2 = `arg = ${user.name || 'the user with no name'}`;
Examples of additional correct code for this rule with { allowNullish: true }
:
const arg = condition ? 'ok' : null;
const msg1 = `arg = ${arg}`;
Examples of additional correct code for this rule with { allowRegExp: true }
:
const arg = new RegExp('foo');
const msg1 = `arg = ${arg}`;
const arg = /foo/;
const msg1 = `arg = ${arg}`;
Examples of additional correct code for this rule with { allowNever: true }
:
const arg = 'something';
const msg1 = typeof arg === 'string' ? arg : `arg = ${arg}`;