no-unnecessary-parameter-property-assignment
Disallow unnecessary assignment of constructor property parameter.
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.
- Flat Config
- Legacy Config
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
- ❌ Incorrect
- ✅ Correct
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.
'## 资源'