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.
import { types } from "node:util"; console.log(types.isPromise(Promise.resolve())); // true console.log(types.isPromise({ then() {} })); // false (only thenable)
Try it
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.
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
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()isfalsefor aDataView, andisArrayBufferView()istruefor both.Bufferis aUint8Array, soisUint8Array(buffer)istrue.isNativeError()is the dependable test for "a real error object", including errors from other realms. A plain object withnameandmessageis not one.isPromise()accepts only real promises. To accept thenables too, test for athenfunction yourself.isAsyncFunction()does not see an ordinary function that returns a promise, only functions declared withasync.isBoxedPrimitive()is fornew String("x")style wrapper objects. A primitive string givesfalse.- For arrays use
Array.isArray(); there is noisArrayhere. - The checks are cheap, but they are not a replacement for duck typing when you want to accept any object with the right behavior.