@typescript-eslint/parameter-properties

Require or disallow parameter properties in class constructors

Also called typescript-eslint/parameter-properties, typescript/parameter-properties

constructor(private name: string) {} declares a field and assigns it in one step. That's short, but the field isn't listed with the class's other fields, so it's easy to miss. It also isn't plain type syntax, so tools that only strip types, such as Node.js, reject it.

By default, every parameter property is reported. The prefer option flips the rule.

Configure#

Off by default. To turn it on:

bunfig.toml
[lint.rules]
"@typescript-eslint/parameter-properties" = "error" # or "warn"

Every option, set to its default:

bunfig.toml
[lint.rules]
"@typescript-eslint/parameter-properties" = ["error", {
  prefer = "class-property", # "class-property" or "parameter-property"
  allow = [], # list of strings
}]

Examples#

example.ts1 problem
class User {
  constructor(private name: string) {}
}
example.tsNo problems
class User {
  private name: string;

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

Options#

OptionDefaultWhat it does
prefer"class-property""parameter-property" flips the rule and reports fields that could be parameter properties.
allow[]Modifiers to leave alone: "readonly", "private", "protected", "public", "private readonly", "protected readonly", "public readonly".

With prefer: "parameter-property", a field is reported if it has no initial value, and the constructor starts by assigning it from a parameter with the same name and the same type:

example.tswith { "prefer": "parameter-property" }1 problem
class User {
  private name: string;

  constructor(name: string) {
    this.name = name;
  }
}
example.tswith { "prefer": "parameter-property" }No problems
class User {
  constructor(private name: string) {}
}

An allow entry must match all the modifiers. "readonly" allows readonly id but not private readonly id:

example.tswith { "allow": ["readonly"] }No problems
class User {
  constructor(readonly id: number) {}
}
example.tswith { "allow": ["readonly"] }1 problem
class User {
  constructor(private readonly id: number) {}
}