Skip to content

constructorReturns

Reports returning values from constructor functions.

✅ This rule is included in the ts untyped preset.

In JavaScript, returning a value from a constructor overrides the newly created instance. This behavior is generally unintentional and can lead to unexpected results where the returned value becomes the result of the new expression instead of the instance.

While JavaScript technically allows this, it’s generally a code smell that indicates a design issue. If you need to return a different object based on constructor parameters, consider using a factory function instead.

class Example {
constructor() {
return {};
}
}
class SpecialValue {
constructor(value: number) {
if (value < 0) {
return null;
}
this.value = value;
}
}

This rule is not configurable.

If you have a rare use case where you intentionally need to return different objects from a constructor, you might choose to disable this rule for those specific cases. However, this is generally considered an anti-pattern, and you should strongly consider refactoring to use a factory function instead.

Made with ❤️‍🔥 in Boston by Josh Goldberg and contributors.