no-constructor-return
Disallow returning a value from a constructor
If a constructor returns an object, new gives the caller that object instead of the new instance, so the result may not be an instance of the class. Returning a primitive such as a number is ignored, or throws a TypeError if the class extends another one.
A bare return; is allowed, so you can leave the constructor early.
Configure#
Off by default. To turn it on:
bunfig.toml
[lint.rules]
"no-constructor-return" = "error" # or "warn"Examples#
example.js1 problem
class Connection {
constructor(url) {
if (cache.has(url)) {
return cache.get(url);Unexpected return statement in constructor.
}
this.url = url;
}
}example.jsNo problems
class Connection {
constructor(url) {
this.url = url;
}
static open(url) {
if (!cache.has(url)) {
cache.set(url, new Connection(url));
}
return cache.get(url);
}
}example.jsNo problems
class Timer {
constructor(delay) {
this.delay = delay;
if (delay === 0) {
return;
}
this.start();
}
}