本文へスキップ

不要なテンプレート式を禁止します。

不要なテンプレート式を禁止します。

🔒

拡張 "plugin:@typescript-eslint/strict-type-checked" ESLint設定 でこのルールを有効にします。

🔧

このルールによって報告される問題の一部は、 --fix ESLintコマンドラインオプション.

によって自動的に修正できます。

このルールには 型情報 が必要です。

このルールは、不要であり、簡素化できる置換式(埋め込み式または文字列補間とも呼ばれます)を含むテンプレートリテラルを報告します。

no-useless-template-literalsからの移行

このルールは、以前はno-useless-template-literalsとして知られていました。古い名前は将来のtypescript-eslintのメジャーバージョンで削除されるため、新しい名前no-unnecessary-template-expressionへの移行をお勧めします。

新しい名前は、機能が同一のドロップイン置換です。

.eslintrc.cjs
module.exports = {
"rules": {
"@typescript-eslint/no-unnecessary-template-expression": "error"
}
};

Playgroundでこのルールを試す ↗

// Static values can be incorporated into the surrounding template.

const ab1 = `${'a'}${'b'}`;
const ab2 = `a${'b'}`;

const stringWithNumber = `${'1 + 1 = '}${2}`;

const stringWithBoolean = `${'true is '}${true}`;

// Some simple expressions that are already strings
// can be rewritten without a template at all.

const text = 'a';
const wrappedText = `${text}`;

declare const intersectionWithString: string & { _brand: 'test-brand' };
const wrappedIntersection = `${intersectionWithString}`;
Playgroundで開く
情報

このルールは、通常の文字列として記述できた置換式のないテンプレートリテラルをフラグすることを目的としていません。つまり、このルールは`this`"this"に変換するのに役立ちません。そのようなルールを探している場合は、@stylistic/ts/quotesルールを構成してこれを行うことができます。

オプション

このルールは設定できません。

使用しない場合

テンプレートリテラル内で文字列式を許可する場合。


型チェック済みlintルールは従来のlintルールよりも強力ですが、型チェック済みlintingの設定も必要です。型チェック済みルールを有効にした後にパフォーマンスの低下が発生した場合は、パフォーマンスのトラブルシューティングを参照してください。

リソース