Skip to content
← Back to rules

typescript/no-inferrable-types スタイル

An auto-fix is available for this rule.

何をするか

数値、文字列、またはブール値に初期化された変数やパラメータに対して明示的な型宣言を禁止します

なぜこれは問題か

リテラル値に初期化された変数やパラメータに明示的に型を指定することは不要です。TypeScript はその値から型を推論できるためです。

このルールに対する誤ったコードの例:

ts
const a: number = 5;
const b: string = "foo";
const c: boolean = true;
const fn = (a: number = 5, b: boolean = true, c: string = "foo") => {};

このルールに対する正しいコードの例:

ts
const a = 5;
const b = "foo";
const c = true;
const fn = (a = 5, b = true, c = "foo") => {};

設定

このルールは以下のプロパティを持つ設定オブジェクトを受け入れます:

ignoreParameters

type: boolean

デフォルト: false

true に設定すると、関数パラメータの型注釈を無視します。

ignoreProperties

type: boolean

デフォルト: false

true に設定すると、クラスのプロパティの型注釈を無視します。

使用方法

設定ファイルまたは CLI でこのルールを有効化するには、以下のように使用できます:

json
{
  "rules": {
    "typescript/no-inferrable-types": "error"
  }
}
bash
oxlint --deny typescript/no-inferrable-types

参考資料