Skip to content
← Back to rules

typescript/no-unnecessary-parameter-property-assignment Correctness

This rule is turned on by default.
💡 A suggestion 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) {}
}

如何使用

To enable this rule using the config file or in the CLI, you can use:

json
{
  "rules": {
    "typescript/no-unnecessary-parameter-property-assignment": "error"
  }
}
ts
import { defineConfig } from "oxlint";

export default defineConfig({
  rules: {
    "typescript/no-unnecessary-parameter-property-assignment": "error",
  },
});
bash
oxlint --deny typescript/no-unnecessary-parameter-property-assignment

版本

此规则添加于 v0.15.13。

参考