@typescript-eslint/no-unnecessary-parameter-property-assignment
Disallow assigning a constructor parameter property to itself
Also called typescript-eslint/no-unnecessary-parameter-property-assignment, typescript/no-unnecessary-parameter-property-assignment
A constructor parameter with public, private, protected or readonly in front of it declares a property and assigns the argument to it. Writing this.name = name in the body does the same work a second time.
Configure#
Off by default. To turn it on:
bunfig.toml
[lint.rules]
"@typescript-eslint/no-unnecessary-parameter-property-assignment" = "error"Examples#
example.ts1 problem
class User {
constructor(public name: string) {
this.name = nameThis assignment is unnecessary since it is already assigned by a parameter property.;
}
}example.tsNo problems
class User {
constructor(public name: string) {}
}An assignment that stores a different value is fine, and so is a copy made after the parameter was changed:
example.tsNo problems
class User {
constructor(
public name: string,
private email: string,
) {
this.name = name.trim();
email = email.toLowerCase();
this.email = email;
}
}