no-unnecessary-parameter-property-assignment
禁止对构造函数属性参数进行不必要的赋值.
TypeScript 的参数属性 允许在一个地方创建和初始化成员。因此,在大多数情况下,没有必要将同名的参数属性分配给构造函数中的成员。
¥TypeScript's parameter properties allow creating and initializing a member in one place. Therefore, in most cases, it is not necessary to assign parameter properties of the same name to members within a constructor.
- 扁平配置
- 旧版配置
eslint.config.mjs
export default tseslint.config({
rules: {
"@typescript-eslint/no-unnecessary-parameter-property-assignment": "error"
}
});
.eslintrc.cjs
module.exports = {
"rules": {
"@typescript-eslint/no-unnecessary-parameter-property-assignment": "error"
}
};
在线运行试试这个规则 ↗
示例
¥Examples
- ❌ 错误
- ✅ 正确
class Foo {
constructor(public bar: string) {
this.bar = bar;
}
}
Open in Playgroundclass Foo {
constructor(public bar: string) {}
}
Open in Playground选项
该规则不可配置。
何时不使用它
¥When Not To Use It
如果你不使用参数属性,则可以忽略此规则。
¥If you don't use parameter properties, you can ignore this rule.