Skip to main content

no-useless-constructor

Disallow unnecessary constructors.

🔒

ESLint 配置 中扩展"plugin:@typescript-eslint/strict" 可启用此规则。

💡

此规则报告的一些问题可以通过编辑器 建议 手动修复。

🧱

This is an "extension" rule that replaces a core ESLint rule to work with TypeScript. See Rules > Extension Rules.

This rule extends the base no-useless-constructor rule from ESLint core. 它增加了对以下内容的支持:

¥It adds support for:

  • 构造函数标记为 protected / private(即将构造函数标记为非公共),

    ¥constructors marked as protected / private (i.e. marking a constructor as non-public),

  • 当没有超类时,public 构造函数,

    ¥public constructors when there is no superclass,

  • 仅具有参数属性的构造函数。

    ¥constructors with only parameter properties.

警告

¥Caveat

此 lint 规则将报告其唯一目的是更改父构造函数的可见性的构造函数。参见 关于该规则缺乏类型信息的讨论 了解上下文。

¥This lint rule will report on constructors whose sole purpose is to change visibility of a parent constructor. See discussion on this rule's lack of type information for context.

选项

See eslint/no-useless-constructor's options.

如何使用

eslint.config.mjs
export default tseslint.config({
rules: {
// Note: you must disable the base rule as it can report incorrect errors
"no-useless-constructor": "off",
"@typescript-eslint/no-useless-constructor": "error"
}
});

在线运行试试这个规则 ↗

'## 资源'

Taken with ❤️ from ESLint core.