Skip to content
← Back to rules

typescript/no-unnecessary-parameter-property-assignment 正确性

This rule is turned on by default.
An auto-fix is available for this rule.

它的作用

防止对参数属性进行不必要的赋值。

为什么这是不好的?

使用公共(public)、私有(private)、受保护(protected)或只读(readonly)之一作为可见性修饰符标记的构造函数参数会自动初始化。显式地提供赋值是多余的,可以移除。

示例

此规则的错误代码示例:

js
class Foo {
  constructor(public name: unknown) {
    this.name = name;
  }
}

此规则的正确代码示例:

js
class Foo {
  constructor(public name: unknown) {}
}

如何使用

通过配置文件或 CLI 启用此规则,可以使用:

json
{
  "rules": {
    "typescript/no-unnecessary-parameter-property-assignment": "error"
  }
}
bash
oxlint --deny typescript/no-unnecessary-parameter-property-assignment

参考资料