# in-operator-type-error

Rule `sonarjs/in-operator-type-error`. "in" should not be used with primitive types

- Source: sonarjs
- Technologies: JavaScript
- Kind: correctness
- Needs type information: yes
- Autofix: no
- Reach: 6.2M weekly downloads of presets that enable it
- Trend: +62.1% last quarter
- Rank: #1224 by reach, #525 by growth

## Linters

| Linter | Rule name | Recommended | Autofix | Details | Docs |
| --- | --- | --- | --- | --- | --- |
| ESLint | `sonarjs/in-operator-type-error` | yes | no | package `eslint-plugin-sonarjs` | [docs](https://rules.sonarsource.com/javascript/RSPEC-3785/) |

## Code examples

### Incorrect: the rule reports this

```js
var x = "Foo";
"length" in x; // Noncompliant: TypeError
0 in x;        // Noncompliant: TypeError
```

### Correct: the rule accepts this

```js
var x = new String("Foo");
"length" in x;    // true
0 in x;           // true
"foobar" in x;    // false
```

Examples from the ESLint documentation.

## Enable it

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

```js
export default [
  {
    rules: {
      "sonarjs/in-operator-type-error": "error",
    },
  },
];
```

## Presets that enable it

| Preset | Linter | Package | Rules | Weekly downloads | Trend |
| --- | --- | --- | --- | --- | --- |
| [SonarJS recommended](https://lintradar.dev/presets/sonarjs-recommended.md) | ESLint | `eslint-plugin-sonarjs` | 230 | 6.2M | +62.1% |

## Related rules

- [sonarjs/hooks-before-test-cases](https://lintradar.dev/rules/sonarjs/hooks-before-test-cases.md): Lifecycle hooks should not be interleaved with test cases or nested suites
- [sonarjs/inconsistent-function-call](https://lintradar.dev/rules/sonarjs/inconsistent-function-call.md): Functions should be called consistently with or without "new"
- [sonarjs/hashing](https://lintradar.dev/rules/sonarjs/hashing.md): Weak hashing algorithms should not be used
- [sonarjs/index-of-compare-to-positive-number](https://lintradar.dev/rules/sonarjs/index-of-compare-to-positive-number.md): "indexOf" checks should not be for positive numbers
- [sonarjs/hardcoded-secret-signatures](https://lintradar.dev/rules/sonarjs/hardcoded-secret-signatures.md): Credentials should not be hard-coded
- [sonarjs/insecure-cookie](https://lintradar.dev/rules/sonarjs/insecure-cookie.md): Cookies should have the "secure" flag

---

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