# prefer-optional-chain

Rule `typescript/prefer-optional-chain`. Enforce using concise optional chain expressions instead of chained logical ands, negated logical ors, or empty objects

- Source: typescript
- Technologies: TypeScript
- Kind: style
- Needs type information: yes
- Autofix: yes, in at least one linter
- Reach: 31.1M weekly downloads of presets that enable it
- Trend: +105.5% last quarter
- Rank: #467 by reach, #262 by growth

## Linters

| Linter | Rule name | Recommended | Autofix | Details | Docs |
| --- | --- | --- | --- | --- | --- |
| ESLint | `@typescript-eslint/prefer-optional-chain` | no | safe | package `@typescript-eslint/eslint-plugin` | [docs](https://typescript-eslint.io/rules/prefer-optional-chain) |
| Oxlint | `typescript/prefer-optional-chain` | no | unsafe | plugin `typescript`, category nursery | [docs](https://oxc.rs/docs/guide/usage/linter/rules/typescript/prefer-optional-chain.html) |
| Biome | `complexity/useOptionalChain` | yes | unsafe | group complexity, since 1.0.0 | [docs](https://biomejs.dev/linter/rules/use-optional-chain/) |

## Code examples

### Incorrect: the rule reports this

```ts
foo && foo.a && foo.a.b && foo.a.b.c;
foo && foo['a'] && foo['a'].b && foo['a'].b.c;
foo && foo.a && foo.a.b && foo.a.b.method && foo.a.b.method();

// With empty objects
(((foo || {}).a || {}).b || {}).c;
(((foo || {})['a'] || {}).b || {}).c;

// With negated `or`s
!foo || !foo.bar;
!foo || !foo[bar];
!foo || !foo.bar || !foo.bar.baz || !foo.bar.baz();

// this rule also supports converting chained strict nullish checks:
foo.a !== null && foo.a !== undefined && foo.a.b;
```

### Correct: the rule accepts this

```ts
foo?.a?.b?.c;
foo?.['a']?.b?.c;
foo?.a?.b?.method?.();

foo?.a?.b?.c?.d?.e;

!foo?.bar;
!foo?.[bar];
!foo?.bar?.baz?.();

foo?.a != null;
```

Examples from the ESLint documentation.

## Enable it

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

```js
import tseslint from "typescript-eslint";

export default [
  {
    plugins: { "@typescript-eslint": tseslint.plugin },
    rules: {
      "@typescript-eslint/prefer-optional-chain": "error",
    },
  },
];
```

### Oxlint: `.oxlintrc.json`

```json
{
  "rules": {
    "typescript/prefer-optional-chain": "error"
  }
}
```

### Biome: `biome.json`

```json
{
  "linter": {
    "rules": {
      "complexity": {
        "useOptionalChain": "error"
      }
    }
  }
}
```

## Presets that enable it

| Preset | Linter | Package | Rules | Weekly downloads | Trend |
| --- | --- | --- | --- | --- | --- |
| [Biome recommended](https://lintradar.dev/presets/biome-recommended.md) | Biome | `@biomejs/biome` | 491 | 28.5M | +106.8% |
| [Ultracite for Biome](https://lintradar.dev/presets/ultracite-biome.md) | Biome | `ultracite` | 616 | 2.1M | +151.7% |
| [XO](https://lintradar.dev/presets/xo.md) | ESLint | `eslint-config-xo` | 618 | 413K | −5.0% |
| [eslint-config-love](https://lintradar.dev/presets/love.md) | ESLint | `eslint-config-love` | 258 | 171.7K | +39.3% |

## Related rules

- [typescript/switch-exhaustiveness-check](https://lintradar.dev/rules/typescript/switch-exhaustiveness-check.md): Require switch-case statements to be exhaustive
- [typescript/prefer-string-starts-ends-with](https://lintradar.dev/rules/typescript/prefer-string-starts-ends-with.md): Enforce using `String#startsWith` and `String#endsWith` over other equivalent methods of checking substrings
- [typescript/consistent-type-exports](https://lintradar.dev/rules/typescript/consistent-type-exports.md): Enforce consistent usage of type exports
- [typescript/ban-types](https://lintradar.dev/rules/typescript/ban-types.md): This rule bans specific types and can suggest alternatives. Note that it does not ban the corresponding runtime objects from being used.
- [typescript/consistent-type-imports](https://lintradar.dev/rules/typescript/consistent-type-imports.md): Enforce consistent usage of type imports
- [typescript/require-await](https://lintradar.dev/rules/typescript/require-await.md): Disallow async functions which do not return promises and have no `await` expression

---

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