noUselessParameterPropertyAssignment (JavaScript)
Language JavaScript (and super languages)
Summary
Section titled “Summary”- Diagnostic Category:
lint/nursery/noUselessParameterPropertyAssignment - This rule doesn’t have a fix.
- The default severity of this rule is information.
- Sources:
How to configure
Section titled “How to configure”{ "linter": { "rules": { "nursery": { "noUselessParameterPropertyAssignment": "error" } } }}Description
Section titled “Description”Disallow assigning a parameter property to itself in the constructor.
A parameter property is a constructor parameter marked with public, protected,
private, readonly, or override. TypeScript creates a class property with the
same name and sets it to the parameter’s value before the constructor body runs.
Writing this.name = name in the constructor body sets the property to the value
it already has. The line does nothing and can be removed.
The rule doesn’t report the assignment if the parameter or the property is changed before it, because then the assignment can change the value of the property.
Examples
Section titled “Examples”Invalid
Section titled “Invalid”class User { constructor(public name: string) { this.name = name; }}code-block.ts:3:9 lint/nursery/noUselessParameterPropertyAssignment ━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━
ℹ This assignment sets the property to the value it already has.
1 │ class User {
2 │ constructor(public name: string) {
> 3 │ this.name = name;
│ ^^^^^^^^^^^^^^^^
4 │ }
5 │ }
ℹ The parameter property already sets this property to the parameter's value before the constructor body runs.
ℹ Remove the assignment.
ℹ This rule belongs to the nursery group, which means it is not yet stable and may change in the future. Visit https://biomejs.dev/linter/#nursery for more information.
class User { constructor(private readonly id?: string) { this.id ??= id; }}code-block.ts:3:9 lint/nursery/noUselessParameterPropertyAssignment ━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━
ℹ This assignment sets the property to the value it already has.
1 │ class User {
2 │ constructor(private readonly id?: string) {
> 3 │ this.id ??= id;
│ ^^^^^^^^^^^^^^
4 │ }
5 │ }
ℹ The parameter property already sets this property to the parameter's value before the constructor body runs.
ℹ Remove the assignment.
ℹ This rule belongs to the nursery group, which means it is not yet stable and may change in the future. Visit https://biomejs.dev/linter/#nursery for more information.
class User { constructor(public name: string) {}}
class Label { constructor(public text: string) { text = text.trim(); this.text = text; }}
class Point { x: number; constructor(x: number) { this.x = x; }}See Also
Section titled “See Also”- If you want to disallow parameter properties entirely, see
noParameterProperties.
Related links
Section titled “Related links”Copyright (c) 2023-present Biome Developers and Contributors.