Skip to content
← Back to rules

eslint/no-constructor-return Pedantic

作用

禁止在构造函数中返回值。

为什么不好?

在 JavaScript 中,在类的构造函数中返回值可能是一个错误。 禁止此模式可防止因不熟悉语言或复制粘贴错误而导致的错误。

示例

此规则不正确代码的示例:

js
class C {
  constructor() {
    return 42;
  }
}

此规则正确代码的示例:

js
class C {
  constructor() {
    this.value = 42;
  }
}

如何使用

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

json
{
  "rules": {
    "no-constructor-return": "error"
  }
}
ts
import { defineConfig } from "oxlint";

export default defineConfig({
  rules: {
    "no-constructor-return": "error",
  },
});
bash
oxlint --deny no-constructor-return

版本

此规则是在 v0.4.3 中添加的。

参考资料