# prefer-class-fields

Rule `unicorn/prefer-class-fields`. Prefer class field declarations over `this` assignments in constructors.

- Source: unicorn
- Technologies: JavaScript
- Kind: style
- Needs type information: no
- Autofix: yes, in at least one linter
- Reach: 12M weekly downloads of presets that enable it
- Trend: +40.1% last quarter
- Rank: #792 by reach, #988 by growth

## Linters

| Linter | Rule name | Recommended | Autofix | Details | Docs |
| --- | --- | --- | --- | --- | --- |
| ESLint | `unicorn/prefer-class-fields` | no | safe | package `eslint-plugin-unicorn` | [docs](https://github.com/sindresorhus/eslint-plugin-unicorn/blob/v80.0.0/docs/rules/prefer-class-fields.md) |
| Oxlint | `unicorn/prefer-class-fields` | no | safe | plugin `unicorn`, category style | [docs](https://oxc.rs/docs/guide/usage/linter/rules/unicorn/prefer-class-fields.html) |

## Code examples

### Incorrect: the rule reports this

```js
class Model {
  constructor() {
    this.items = [];
    this.options = {enabled: true};
  }
}
```

```js
class Foo {
  foo = 'foo';
  constructor() {
    this.foo = 'bar';
  }
}
```

```js
class Foo {
  #foo = 'foo';
  constructor() {
    this.#foo = 'bar';
  }
}
```

### Correct: the rule accepts this

```js
class Model {
  items = [];
  options = {enabled: true};
}
```

```js
class Foo {
  foo = 'bar';
}
```

```js
class Foo {
  #foo = 'bar';
}
```

Examples from the ESLint documentation.

## Enable it

### ESLint: `eslint.config.js`

```js
import unicorn from "eslint-plugin-unicorn";

export default [
  {
    plugins: { unicorn },
    rules: {
      "unicorn/prefer-class-fields": "error",
    },
  },
];
```

### Oxlint: `.oxlintrc.json`

```json
{
  "rules": {
    "unicorn/prefer-class-fields": "error"
  }
}
```

## Presets that enable it

| Preset | Linter | Package | Rules | Weekly downloads | Trend |
| --- | --- | --- | --- | --- | --- |
| [Unicorn recommended](https://lintradar.dev/presets/unicorn-recommended.md) | ESLint | `eslint-plugin-unicorn` | 340 | 9.5M | +30.3% |
| [Unicorn unopinionated](https://lintradar.dev/presets/unicorn-unopinionated.md) | ESLint | `eslint-plugin-unicorn` | 236 | 9.5M | +30.3% |
| [Ultracite for Oxlint](https://lintradar.dev/presets/ultracite-oxlint.md) | Oxlint | `ultracite` | 475 | 2.1M | +151.7% |
| [XO](https://lintradar.dev/presets/xo.md) | ESLint | `eslint-config-xo` | 618 | 413K | −5.0% |

## Related rules

- [unicorn/prefer-blob-reading-methods](https://lintradar.dev/rules/unicorn/prefer-blob-reading-methods.md): Prefer `Blob#arrayBuffer()` over `FileReader#readAsArrayBuffer(…)` and `Blob#text()` over `FileReader#readAsText(…)`.
- [unicorn/prefer-classlist-toggle](https://lintradar.dev/rules/unicorn/prefer-classlist-toggle.md): Prefer using `Element#classList.toggle()` to toggle class names.
- [unicorn/prefer-array-flat](https://lintradar.dev/rules/unicorn/prefer-array-flat.md): Prefer `Array#flat()` over legacy techniques to flatten arrays.
- [unicorn/prefer-code-point](https://lintradar.dev/rules/unicorn/prefer-code-point.md): Prefer `String#codePointAt(…)` over `String#charCodeAt(…)` and `String.fromCodePoint(…)` over `String.fromCharCode(…)`.
- [unicorn/prefer-array-find](https://lintradar.dev/rules/unicorn/prefer-array-find.md): Prefer `.find(…)` and `.findLast(…)` over the first or last element from `.filter(…)`.
- [unicorn/prefer-default-parameters](https://lintradar.dev/rules/unicorn/prefer-default-parameters.md): Prefer default parameters and destructuring defaults over reassignment and fallback expressions.

---

Web page: https://lintradar.dev/rules/unicorn/prefer-class-fields. Data from npm and the linters' documentation, collected Dec 4, 2026. Site guide for agents: https://lintradar.dev/llms.txt
