Skip to content

useAccessorPairs (JavaScript)

Language JavaScript (and super languages)
biome.json
{
"linter": {
"rules": {
"nursery": {
"useAccessorPairs": "error"
}
}
}
}

Require a getter for every setter in objects and classes.

A property that has a setter but no getter can be written but not read: reading it always returns undefined. This is rarely what you want and usually means the getter was forgotten.

This rule checks setters in object literals and classes. Static and instance members of a class are checked separately, so a static getter doesn’t pair with an instance setter.

The rule also checks property descriptors passed to Object.create(), Object.defineProperty(), Object.defineProperties(), and Reflect.defineProperty(): a descriptor with a set function must also have a get function.

Getters without setters are allowed, because read-only properties are common.

const user = {
set name(value) {
this._name = value;
},
};
code-block.js:2:5 lint/nursery/useAccessorPairs ━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━

ℹ This setter has no matching getter.

1 │ const user = {
> 2 │ set name(value) {
│ ^^^^^^^^
3 │ this._name = value;
4 │ },

ℹ Without a getter, reading the property always returns undefined.

ℹ Add a getter for the property, or remove the setter.

ℹ This rule belongs to the nursery group, which means it is not yet stable and may change in the future. Visit https://biomejs.dev/linter/#nursery for more information.

class User {
set name(value) {
this._name = value;
}
}
code-block.js:2:5 lint/nursery/useAccessorPairs ━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━

ℹ This setter has no matching getter.

1 │ class User {
> 2 │ set name(value) {
│ ^^^^^^^^
3 │ this._name = value;
4 │ }

ℹ Without a getter, reading the property always returns undefined.

ℹ Add a getter for the property, or remove the setter.

ℹ This rule belongs to the nursery group, which means it is not yet stable and may change in the future. Visit https://biomejs.dev/linter/#nursery for more information.

Object.defineProperty(user, "name", {
set(value) {
this._name = value;
},
});
code-block.js:2:5 lint/nursery/useAccessorPairs ━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━

ℹ This property descriptor has a setter but no getter.

1 │ Object.defineProperty(user, "name", {
> 2 │ set(value) {
│ ^^^
3 │ this._name = value;
4 │ },

ℹ Without a getter, reading the property always returns undefined.

ℹ Add a getter for the property, or remove the setter.

ℹ This rule belongs to the nursery group, which means it is not yet stable and may change in the future. Visit https://biomejs.dev/linter/#nursery for more information.

const user = {
get name() {
return this._name;
},
set name(value) {
this._name = value;
},
};
class Counter {
get count() {
return this._count;
}
}
Object.defineProperty(user, "name", {
get() {
return this._name;
},
set(value) {
this._name = value;
},
});