Skip to content

constructorReturns

Reports returning values from constructor functions.

✅ This rule is included in the ts untyped presets.

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
class Example
Example
{
constructor() {
return {};
}
}
class
class SpecialValue
SpecialValue
{
constructor(
value: number
value
: number) {
if (
value: number
value
< 0) {
return null;
Error ts(2322) ― Type 'null' is not assignable to type 'SpecialValue'.
Error ts(2409) ― Return type of constructor signature must be assignable to the instance type of the class.
}
this.value =
value: number
value
;
Error ts(2339) ― Property 'value' does not exist on type 'SpecialValue'.
}
}

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 ❤️‍🔥 around the world by the Flint team and contributors.