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.
Examples
Section titled “Examples”class Example { constructor() { return {}; }}
class SpecialValue { constructor(value: number) { if (value < 0) { return null; } this.value = value; }}
class Example { constructor() { this.value = 42; }}
class SpecialValue { constructor(value: number) { if (value < 0) { throw new Error("Value must be non-negative"); } this.value = value; }}
Options
Section titled “Options”This rule is not configurable.
When Not To Use It
Section titled “When Not To Use It”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.
Further Reading
Section titled “Further Reading”Equivalents in Other Linters
Section titled “Equivalents in Other Linters”- Biome:
noConstructorReturn
- ESLint:
no-constructor-return
- Oxlint:
eslint/no-constructor-return