不要なテンプレート式を禁止します。
不要なテンプレート式を禁止します。
🔒
拡張 "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で開く// Static values can be incorporated into the surrounding template.
const ab1 = `ab`;
const ab2 = `ab`;
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の設定も必要です。型チェック済みルールを有効にした後にパフォーマンスの低下が発生した場合は、パフォーマンスのトラブルシューティングを参照してください。