src/content/docs/linter/rules/no-constructor-return.mdx
import { Tabs, TabItem } from '@astrojs/starlight/components';
<Tabs> <TabItem label="JavaScript (and super languages)" icon="seti:javascript"> ## Summary - Rule available since: `v1.0.0` - Diagnostic Category: [`lint/correctness/noConstructorReturn`](/reference/diagnostics#diagnostic-category) - This rule is **recommended**, meaning it is enabled by default. - This rule doesn't have a fix. - The default severity of this rule is [**error**](/reference/diagnostics#error). - Sources: - Same as [`no-constructor-return`](https://eslint.org/docs/latest/rules/no-constructor-return){
"linter": {
"rules": {
"correctness": {
"noConstructorReturn": "error"
}
}
}
}
Disallow returning a value from a constructor.
Returning a value from a constructor of a class is a possible error.
Forbidding this pattern prevents errors resulting from unfamiliarity with JavaScript or a copy-paste error.
Only returning without a value is allowed, as it’s a control flow statement.
class A {
constructor() {
return 0;
}
}
class A {
constructor() {}
}
class B {
constructor(x) {
return;
}
}
Some people implement the singleton pattern in JavaScript by returning an existing instance from the constructor, which would conflict with this rule.
Instead, we advise to follow one of the suggestions described in this blog post: https://arendjr.nl/blog/2024/11/singletons-in-javascript/