noConstructorReturn (since v11.0.0)
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.
Source: https://eslint.org/docs/latest/rules/no-constructor-return
Examples
Invalid
class A {
constructor() {
return 0;
}
}
nursery/noConstructorReturn.js:3:9 lint/nursery/noConstructorReturn ━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━
✖ The constructor should not return a value.
1 │ class A {
2 │ constructor() {
> 3 │ return 0;
│ ^^^^^^^^^
4 │ }
5 │ }
ℹ The constructor is here:
1 │ class A {
> 2 │ constructor() {
│ ^^^^^^^^^^^^^^^
> 3 │ return 0;
> 4 │ }
│ ^
5 │ }
6 │
ℹ Returning a value from a constructor is ignored.
Valid
class A {
constructor() {}
}
class B {
constructor(x) {
return;
}
}