|
| 1 | +# Disallow `instanceof` with built-in objects |
| 2 | + |
| 3 | +💼 This rule is enabled in the ✅ `recommended` [config](https://github.com/sindresorhus/eslint-plugin-unicorn#preset-configs-eslintconfigjs). |
| 4 | + |
| 5 | +🔧 This rule is automatically fixable by the [`--fix` CLI option](https://eslint.org/docs/latest/user-guide/command-line-interface#--fix). |
| 6 | + |
| 7 | +<!-- end auto-generated rule header --> |
| 8 | +<!-- Do not manually modify this header. Run: `npm run fix:eslint-docs` --> |
| 9 | + |
| 10 | +Using `instanceof` to determine the type of an object has [limitations](https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Operators/instanceof#instanceof_and_multiple_realms). |
| 11 | + |
| 12 | +Therefore, it is recommended to use a safer method, like `Object.prototype.toString.call(foo)` or the npm package [@sindresorhus/is](https://www.npmjs.com/package/@sindresorhus/is) to determine the type of an object. |
| 13 | + |
| 14 | +## Examples |
| 15 | + |
| 16 | +```js |
| 17 | +foo instanceof String; // ❌ |
| 18 | +typeof foo === 'string'; // ✅ |
| 19 | +``` |
| 20 | + |
| 21 | +```js |
| 22 | +foo instanceof Number; // ❌ |
| 23 | +typeof foo === 'number'; // ✅ |
| 24 | +``` |
| 25 | + |
| 26 | +```js |
| 27 | +foo instanceof Boolean; // ❌ |
| 28 | +typeof foo === 'boolean'; // ✅ |
| 29 | +``` |
| 30 | + |
| 31 | +```js |
| 32 | +foo instanceof BigInt; // ❌ |
| 33 | +typeof foo === 'bigint'; // ✅ |
| 34 | +``` |
| 35 | + |
| 36 | +```js |
| 37 | +foo instanceof Symbol; // ❌ |
| 38 | +typeof foo === 'symbol'; // ✅ |
| 39 | +``` |
| 40 | + |
| 41 | +```js |
| 42 | +foo instanceof Array; // ❌ |
| 43 | +Array.isArray(foo); // ✅ |
| 44 | +``` |
| 45 | + |
| 46 | +```js |
| 47 | +foo instanceof Function; // ❌ |
| 48 | +typeof foo === 'function'; // ✅ |
| 49 | +``` |
| 50 | + |
| 51 | +```js |
| 52 | +foo instanceof Object; // ❌ |
| 53 | +Object.prototype.toString.call(foo) === '[object Object]'; // ✅ |
| 54 | +``` |
| 55 | + |
| 56 | +```js |
| 57 | +import is from '@sindresorhus/is'; |
| 58 | + |
| 59 | +foo instanceof Map; // ❌ |
| 60 | +is(foo) === 'Map'; // ✅ |
| 61 | +``` |
| 62 | + |
| 63 | +## Options |
| 64 | + |
| 65 | +### strategy |
| 66 | + |
| 67 | +Type: `'loose' | 'strict'`\ |
| 68 | +Default: `'loose'` |
| 69 | + |
| 70 | +The matching strategy: |
| 71 | + |
| 72 | +- `'loose'` - Matches the primitive type (`string`, `number`, `boolean`, `bigint`, `symbol`) constructors, `Function`, and `Array`. |
| 73 | +- `'strict'` - Matches all built-in constructors. |
| 74 | + |
| 75 | +```js |
| 76 | +"unicorn/no-instanceof-builtin-object": [ |
| 77 | + "error", |
| 78 | + { |
| 79 | + "strategy": "strict" |
| 80 | + } |
| 81 | +] |
| 82 | +``` |
| 83 | + |
| 84 | +### include |
| 85 | + |
| 86 | +Type: `string[]`\ |
| 87 | +Default: `[]` |
| 88 | + |
| 89 | +Specify the constructors that should be validated. |
| 90 | + |
| 91 | +```js |
| 92 | +"unicorn/no-instanceof-builtin-object": [ |
| 93 | + "error", |
| 94 | + { |
| 95 | + "include": [ |
| 96 | + "WebWorker", |
| 97 | + "HTMLElement" |
| 98 | + ] |
| 99 | + } |
| 100 | +] |
| 101 | +``` |
| 102 | + |
| 103 | +### exclude |
| 104 | + |
| 105 | +Type: `string[]`\ |
| 106 | +Default: `[]` |
| 107 | + |
| 108 | +Specifies the constructors that should be excluded, with this rule taking precedence over others. |
| 109 | + |
| 110 | +```js |
| 111 | +"unicorn/no-instanceof-builtin-object": [ |
| 112 | + "error", |
| 113 | + { |
| 114 | + "exclude": [ |
| 115 | + "String", |
| 116 | + "Number" |
| 117 | + ] |
| 118 | + } |
| 119 | +] |
| 120 | +``` |
| 121 | + |
| 122 | +### useErrorIsError |
| 123 | + |
| 124 | +Type: `boolean`\ |
| 125 | +Default: `false` |
| 126 | + |
| 127 | +Specifies using [`Error.isError()`](https://github.com/tc39/proposal-is-error) to determine whether it is an error object. |
| 128 | + |
| 129 | +```js |
| 130 | +"unicorn/no-instanceof-builtin-object": [ |
| 131 | + "error", |
| 132 | + { |
| 133 | + "strategy": "strict", |
| 134 | + "useErrorIsError": true |
| 135 | + } |
| 136 | +] |
| 137 | +``` |
| 138 | + |
| 139 | +This option will be removed at some point in the future. |
0 commit comments