Skip to content
← Back to rules

typescript/parameter-properties スタイル

何をするか

クラスのコンストラクタ内でのパラメータプロパティの使用を許可または禁止します。

なぜ問題なのか

パラメータプロパティとクラスプロパティ宣言を混在させると、クラスのスタイルが一貫性を失い、保守が難しくなることがあります。

{ "prefer": "class-property" }(デフォルト)

このルールに不適切なコード例:

ts
class Foo {
  constructor(private name: string) {}
}

このルールに適切なコード例:

ts
class Foo {
  name: string;
  constructor(name: string) {
    this.name = name;
  }
}

{ "prefer": "parameter-property" }

このルールに不適切なコード例:

ts
class Foo {
  name: string;
  constructor(name: string) {
    this.name = name;
  }
}

このルールに適切なコード例:

ts
class Foo {
  constructor(private name: string) {}
}

設定

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

allow

type: array

default: []

prefer オプションに応じて、パラメータプロパティまたはクラスプロパティと一緒に使用できる修飾子。

allow[n]

type: "private" | "private readonly" | "protected" | "protected readonly" | "public" | "public readonly" | "readonly"

prefer

type: "class-property" | "parameter-property"

default: "class-property"

パラメータプロパティかクラスプロパティのどちらを優先するか。

使用方法

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

json
{
  "rules": {
    "typescript/parameter-properties": "error"
  }
}
bash
oxlint --deny typescript/parameter-properties

参照