# max-union-size

Rule `sonarjs/max-union-size`. Union types should not have too many elements

- Source: sonarjs
- Technologies: JavaScript
- Kind: style
- Needs type information: no
- Autofix: no
- Reach: 0 weekly downloads of presets that enable it
- Trend: 0% last quarter
- Rank: #2141 by reach

## Linters

| Linter | Rule name | Recommended | Autofix | Details | Docs |
| --- | --- | --- | --- | --- | --- |
| ESLint | `sonarjs/max-union-size` | no | no | package `eslint-plugin-sonarjs` | [docs](https://rules.sonarsource.com/javascript/RSPEC-4622/) |

## Code examples

### Incorrect: the rule reports this

```js
let x: MyType1 | MyType2 | MyType3 | MyType4; // Noncompliant

function foo(p1: string, p2: MyType1 | MyType2 | MyType3 | MyType4) { // Noncompliant
    // ...
}
```

### Correct: the rule accepts this

```js
type MyUnionType = MyType1 | MyType2 | MyType3 | MyType4; // Compliant, "type" statements are ignored
let x: MyUnionType;

function foo(value: string, padding: MyUnionType) {
    // ...
}
```

Examples from the ESLint documentation.

## Enable it

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

```js
export default [
  {
    rules: {
      "sonarjs/max-union-size": "error",
    },
  },
];
```

## Related rules

- [sonarjs/max-lines-per-function](https://lintradar.dev/rules/sonarjs/max-lines-per-function.md): Functions should not have too many lines of code
- [sonarjs/nested-control-flow](https://lintradar.dev/rules/sonarjs/nested-control-flow.md): Control flow statements "if", "for", "while", "switch" and "try" should not be nested too deeply
- [sonarjs/max-lines](https://lintradar.dev/rules/sonarjs/max-lines.md): Files should not have too many lines of code
- [sonarjs/no-built-in-override](https://lintradar.dev/rules/sonarjs/no-built-in-override.md): Built-in objects should not be overridden
- [sonarjs/no-collapsible-if](https://lintradar.dev/rules/sonarjs/no-collapsible-if.md): Mergeable "if" statements should be combined
- [sonarjs/function-name](https://lintradar.dev/rules/sonarjs/function-name.md): Function and method names should comply with a naming convention

---

Web page: https://lintradar.dev/rules/sonarjs/max-union-size. Data from npm and the linters' documentation, collected Dec 4, 2026. Site guide for agents: https://lintradar.dev/llms.txt
