no-instanceof-builtins

March 27, 2026 ยท View on GitHub

๐Ÿ“ Disallow instanceof with built-in objects.

๐Ÿ’ผ This rule is enabled in the following configs: โœ… recommended, โ˜‘๏ธ unopinionated.

๐Ÿ”ง๐Ÿ’ก This rule is automatically fixable by the --fix CLI option and manually fixable by editor suggestions.

Using instanceof to determine the type of an object has limitations.

Therefore, it is recommended to use a safer method, like Object.prototype.toString.call(foo) or the npm package @sindresorhus/is to determine the type of an object.

Examples

// โŒ
foo instanceof String;

// โœ…
typeof foo === 'string';
// โŒ
foo instanceof Number;

// โœ…
typeof foo === 'number';
// โŒ
foo instanceof Boolean;

// โœ…
typeof foo === 'boolean';
// โŒ
foo instanceof BigInt;

// โœ…
typeof foo === 'bigint';
// โŒ
foo instanceof Symbol;

// โœ…
typeof foo === 'symbol';
// โŒ
foo instanceof Array;

// โœ…
Array.isArray(foo);
// โŒ
foo instanceof Function;

// โœ…
typeof foo === 'function';
// โŒ
foo instanceof Object;

// โœ…
Object.prototype.toString.call(foo) === '[object Object]';
import is from '@sindresorhus/is';

// โŒ
foo instanceof Map;

// โœ…
is(foo) === 'Map';

Options

strategy

Type: 'loose' | 'strict'
Default: 'loose'

The matching strategy:

  • 'loose' - Matches the primitive type (string, number, boolean, bigint, symbol) constructors, Function, and Array.
  • 'strict' - Matches all built-in constructors.
"unicorn/no-instanceof-builtins": [
	"error",
	{
		"strategy": "strict"
	}
]

include

Type: string[]
Default: []

Specify the constructors that should be validated.

"unicorn/no-instanceof-builtins": [
	"error",
	{
		"include": [
			"WebWorker",
			"HTMLElement"
		]
	}
]

exclude

Type: string[]
Default: []

Specifies the constructors that should be excluded, with this rule taking precedence over others.

"unicorn/no-instanceof-builtins": [
	"error",
	{
		"exclude": [
			"String",
			"Number"
		]
	}
]

useErrorIsError

Type: boolean
Default: false

Specifies using Error.isError() to determine whether it is an error object.

"unicorn/no-instanceof-builtins": [
	"error",
	{
		"strategy": "strict",
		"useErrorIsError": true
	}
]

This option will be removed at some point in the future.