Skip to content

setterReturns

Reports return statements with values inside setters.

✅ This rule is included in the ts untyped presets.

The set syntax binds an object property to a function that will be called when there is an attempt to set that property. Setters are expected to have side effects only and not produce a return value. Any value returned from a setter is ignored by the JavaScript engine.

This rule reports when a setter contains a return statement with a value.

const
const object: {
value: any;
}
object
= {
set
value: any
value
(
val: any
val
) {
return
val: any
val
;
Error ts(2408) ― Setters cannot return a value.
},
};
class
class Example
Example
{
set
Example.name: any
name
(
value: any
value
) {
return
value: any
value
;
Error ts(2408) ― Setters cannot return a value.
}
}
class
class Example
Example
{
set
Example.value: any
value
(
val: any
val
) {
if (
val: any
val
> 0) {
return
val: any
val
;
Error ts(2408) ― Setters cannot return a value.
}
this._value =
val: any
val
;
Error ts(2551) ― Property '_value' does not exist on type 'Example'. Did you mean 'value'?
}
}

This rule is not configurable.

If you are using an unusual non-standard tool that modifiers your setters at build time into standard functions, this rule may not be for you.

Made with ❤️‍🔥 around the world by the Flint team and contributors.