Typed Array Kind Detector

1.1.20 · active · verified Tue Apr 21

The `which-typed-array` package provides a robust and cross-realm method for determining the specific kind of JavaScript Typed Array a given value is. Unlike `instanceof`, it works reliably across different JavaScript realms (e.g., iframes, web workers) and bypasses issues with `Symbol.toStringTag` being overridden. It returns the string name of the Typed Array (e.g., 'Int8Array', 'Uint32Array', 'Float64Array') or `false` if the value is not a Typed Array. The current stable version is 1.1.20, indicating a mature and well-tested utility. Given its focused scope and the stability of the underlying JavaScript `TypedArray` specification, new releases are generally infrequent, focusing on compatibility and minor improvements. It's a foundational utility often used by libraries that need to handle various JavaScript value types precisely.

Common errors

Warnings

Install

Imports

Quickstart

This quickstart demonstrates how to import and use `whichTypedArray` to identify various Typed Array types and distinguish them from other JavaScript values.

import whichTypedArray from 'which-typed-array';

console.log(whichTypedArray(new Int8Array())); // 'Int8Array'
console.log(whichTypedArray(new Uint8ClampedArray(10))); // 'Uint8ClampedArray'
console.log(whichTypedArray(new Float64Array([1.1, 2.2]))); // 'Float64Array'
console.log(whichTypedArray([])); // false
console.log(whichTypedArray({})); // false
console.log(whichTypedArray(42)); // false
console.log(whichTypedArray(null)); // false

// Example with BigInt Typed Arrays
if (typeof BigInt64Array !== 'undefined') {
  console.log(whichTypedArray(new BigInt64Array([1n, 2n]))); // 'BigInt64Array'
}

view raw JSON →