instanceof
operator in JavaScript has some limitations.
To detect the type of a variable, we should encourage user to use:
typeof
operatortypeof foo === 'string' // detect whether foo is string
node:util/types
module (for Node)import {isNumberObject, isMap} from 'node:util/types';
isNumberObject(new Number(123)) // true
isNumberObject(123) // false
isMap(new Map()) // true
@sindresorhus/is
module (for browser)import is from '@sindresorhus/is';
is(new Map()) === 'Map' // true
Therefore, builtin objects like Map
/Promise
/Error
can be detected by typeof
, node:util/types
and @sindresorhus/is
. Should not use instanceof
.
Rule no-instanceof-array
can be removed if this rule is implemented.
foo instanceof Array
foo instanceof Map
foo instanceof Promise
import types from 'node:util/types'
Array.isArray(foo)
types.isMap(foo)
types.isPromise(foo)
import is from '@sindresorhus/is'
is(foo) === 'Array'
is(foo) === 'Map'
is(foo) === 'Promise'
no-instanceof-builtin-object
No response
Pay now to fund the work behind this issue.
Get updates on progress being made.
Maintainer is rewarded once the issue is completed.
You're funding impactful open source efforts
You want to contribute to this effort
You want to get funding like this too