typescript/no-unnecessary-template-expression 懸念
何を実行するか
不要なテンプレート式(インターポレーション)を禁止します。これらの式は簡略化できる場合があります。
なぜ問題なのか?
不要な置換式を含むテンプレートリテラルは、利点を提供せずに複雑さを追加します。静的文字列リテラル、または既に文字列である式は簡略化できます。
注意:このルールは、置換式のないテンプレートリテラルを警告しません。たとえば、`hello` は 'hello' と書けるにもかかわらず、許可されています。
例
このルールに対して不正なコードの例:
ts
// 静的値は周囲のテンプレートに組み込むことができる
const ab1 = `${"a"}${"b"}`;
const ab2 = `a${"b"}`;
const stringWithNumber = `${"1 + 1 = "}${2}`;
const stringWithBoolean = `${"true is "}${true}`;
// すでに文字列である式は、テンプレートを使わず書き直せる
const text = "a";
const wrappedText = `${text}`;
declare const intersectionWithString: string & { _brand: "test-brand" };
const wrappedIntersection = `${intersectionWithString}`;このルールに対して正しいコードの例:
ts
// テンプレートに統合された静的値
const ab1 = `ab`;
// 非自明なインターポレーションを含むテンプレート
const name = "world";
const greeting = `Hello ${name}!`;
// 式を含むテンプレート
const result = `Result: ${1 + 2}`;
// 単純な文字列にはテンプレートは不要
const text = "a";
const wrappedText = text;
// 複数行文字列は問題ありません
const multiline = `
Hello
world
`;使用方法
設定ファイルまたは CLI でこのルールを有効化するには、次のように使用できます:
json
{
"rules": {
"typescript/no-unnecessary-template-expression": "error"
}
}bash
oxlint --type-aware --deny typescript/no-unnecessary-template-expression