Skip to content
Tutorials Center
My learning

Topics

Node.js Reference ยท util, assert and the Test Runner

Node.js util.types object

Reference for util.types in Node.js: isPromise, isProxy, isNativeError, isTypedArray, isMap and dozens of other checks that work across realms and proxies.

util.types is a collection of functions that answer "what kind of built-in object is this?" more reliably than instanceof or typeof. Each function takes one value and returns a boolean.

instanceof fails for objects created in another realm (a vm context, an iframe-like sandbox) and can be fooled by Symbol.hasInstance. The checks in util.types look at the internal type of the object instead, so they also see through some cases that user code cannot detect, such as a Proxy. Import it as import { types } from "node:util", or from node:util/types.

NODE
import { types } from "node:util";

console.log(types.isPromise(Promise.resolve())); // true
console.log(types.isPromise({ then() {} }));     // false (only thenable)

Try it

Node.js Checks that instanceof cannot do

Press Run (or Ctrl + Enter) to see the result here.

Program input ยท what you'd type while the program runs

Type the answers before you press Run, one per line. Each time the program reads a line (with node:readline or process.stdin), it gets the next line from this box.

Node.js A tour of common checks

Press Run (or Ctrl + Enter) to see the result here.

Program input ยท what you'd type while the program runs

Type the answers before you press Run, one per line. Each time the program reads a line (with node:readline or process.stdin), it gets the next line from this box.

Syntax

NODE
util.types.isXxx(value: unknown): boolean
Name Description
value Any value. Primitives simply return false for object checks.

Common groups of checks:

Group Functions
Collections isMap, isSet, isWeakMap, isWeakSet, isMapIterator, isSetIterator
Binary data isArrayBuffer, isSharedArrayBuffer, isAnyArrayBuffer, isArrayBufferView, isTypedArray, isUint8Array and one per typed array, isDataView
Functions isAsyncFunction, isGeneratorFunction, isGeneratorObject
Wrappers isBoxedPrimitive, isNumberObject, isStringObject, isBooleanObject, isBigIntObject, isSymbolObject
Other isPromise, isProxy, isNativeError, isDate, isRegExp, isModuleNamespaceObject, isKeyObject, isCryptoKey

Returns: true or false. None of these functions throw.

Usage notes

  • isTypedArray() is false for a DataView, and isArrayBufferView() is true for both. Buffer is a Uint8Array, so isUint8Array(buffer) is true.
  • isNativeError() is the dependable test for "a real error object", including errors from other realms. A plain object with name and message is not one.
  • isPromise() accepts only real promises. To accept thenables too, test for a then function yourself.
  • isAsyncFunction() does not see an ordinary function that returns a promise, only functions declared with async.
  • isBoxedPrimitive() is for new String("x") style wrapper objects. A primitive string gives false.
  • For arrays use Array.isArray(); there is no isArray here.
  • The checks are cheap, but they are not a replacement for duck typing when you want to accept any object with the right behavior.