Type check values with comprehensive TypeScript type guards and runtime assertions
—
Type checking for typed arrays, array buffers, and binary data structures including all variants of typed arrays and related buffer types.
Check if a value is any type of typed array.
/**
* Check if value is any typed array
* @param value - Value to check
* @returns True if value is a typed array
*/
function isTypedArray(value: unknown): value is TypedArray;
type TypedArray =
| Int8Array
| Uint8Array
| Uint8ClampedArray
| Int16Array
| Uint16Array
| Int32Array
| Uint32Array
| Float32Array
| Float64Array
| BigInt64Array
| BigUint64Array;Usage Examples:
import is from '@sindresorhus/is';
is.typedArray(new Uint8Array()); // => true
is.typedArray(new Float32Array()); // => true
is.typedArray(new BigInt64Array()); // => true
is.typedArray([]); // => false
is.typedArray(new ArrayBuffer(8)); // => false
// Type guard usage
function processTypedArray(value: unknown) {
if (is.typedArray(value)) {
// value is now typed as TypedArray
console.log('Length:', value.length);
console.log('Byte length:', value.byteLength);
console.log('Buffer:', value.buffer);
}
}Type checking for 8-bit integer typed arrays.
/**
* Check if value is Int8Array
* @param value - Value to check
* @returns True if value is Int8Array
*/
function isInt8Array(value: unknown): value is Int8Array;
/**
* Check if value is Uint8Array
* @param value - Value to check
* @returns True if value is Uint8Array
*/
function isUint8Array(value: unknown): value is Uint8Array;
/**
* Check if value is Uint8ClampedArray
* @param value - Value to check
* @returns True if value is Uint8ClampedArray
*/
function isUint8ClampedArray(value: unknown): value is Uint8ClampedArray;Usage Examples:
import is from '@sindresorhus/is';
// Int8Array: -128 to 127
const int8 = new Int8Array([127, -128]);
is.int8Array(int8); // => true
// Uint8Array: 0 to 255
const uint8 = new Uint8Array([0, 255]);
is.uint8Array(uint8); // => true
// Uint8ClampedArray: 0 to 255 (clamped)
const clamped = new Uint8ClampedArray([0, 255, 300]); // 300 becomes 255
is.uint8ClampedArray(clamped); // => true
// Cross-type checking
is.uint8Array(int8); // => false
is.int8Array(uint8); // => falseType checking for 16-bit integer typed arrays.
/**
* Check if value is Int16Array
* @param value - Value to check
* @returns True if value is Int16Array
*/
function isInt16Array(value: unknown): value is Int16Array;
/**
* Check if value is Uint16Array
* @param value - Value to check
* @returns True if value is Uint16Array
*/
function isUint16Array(value: unknown): value is Uint16Array;Usage Examples:
import is from '@sindresorhus/is';
// Int16Array: -32,768 to 32,767
const int16 = new Int16Array([32767, -32768]);
is.int16Array(int16); // => true
// Uint16Array: 0 to 65,535
const uint16 = new Uint16Array([0, 65535]);
is.uint16Array(uint16); // => true
// Working with binary data
function processAudioSamples(data: unknown) {
if (is.int16Array(data)) {
// data is now typed as Int16Array
for (let i = 0; i < data.length; i++) {
// Process 16-bit audio sample
const sample = data[i] / 32767; // Normalize to -1 to 1
console.log(sample);
}
}
}Type checking for 32-bit integer typed arrays.
/**
* Check if value is Int32Array
* @param value - Value to check
* @returns True if value is Int32Array
*/
function isInt32Array(value: unknown): value is Int32Array;
/**
* Check if value is Uint32Array
* @param value - Value to check
* @returns True if value is Uint32Array
*/
function isUint32Array(value: unknown): value is Uint32Array;Usage Examples:
import is from '@sindresorhus/is';
// Int32Array: -2,147,483,648 to 2,147,483,647
const int32 = new Int32Array([2147483647, -2147483648]);
is.int32Array(int32); // => true
// Uint32Array: 0 to 4,294,967,295
const uint32 = new Uint32Array([0, 4294967295]);
is.uint32Array(uint32); // => true
// Type guard usage
function processPixelData(data: unknown) {
if (is.uint32Array(data)) {
// data is now typed as Uint32Array
for (let i = 0; i < data.length; i++) {
const pixel = data[i];
const r = (pixel >> 24) & 0xFF;
const g = (pixel >> 16) & 0xFF;
const b = (pixel >> 8) & 0xFF;
const a = pixel & 0xFF;
console.log(`RGBA: ${r}, ${g}, ${b}, ${a}`);
}
}
}Type checking for floating-point typed arrays.
/**
* Check if value is Float32Array
* @param value - Value to check
* @returns True if value is Float32Array
*/
function isFloat32Array(value: unknown): value is Float32Array;
/**
* Check if value is Float64Array
* @param value - Value to check
* @returns True if value is Float64Array
*/
function isFloat64Array(value: unknown): value is Float64Array;Usage Examples:
import is from '@sindresorhus/is';
// Float32Array: single precision floating point
const float32 = new Float32Array([3.14, -2.71]);
is.float32Array(float32); // => true
// Float64Array: double precision floating point
const float64 = new Float64Array([Math.PI, Math.E]);
is.float64Array(float64); // => true
// Working with scientific data
function processData(data: unknown) {
if (is.float64Array(data)) {
// data is now typed as Float64Array
const sum = data.reduce((acc, val) => acc + val, 0);
const average = sum / data.length;
console.log('Average:', average);
}
}Type checking for BigInt typed arrays.
/**
* Check if value is BigInt64Array
* @param value - Value to check
* @returns True if value is BigInt64Array
*/
function isBigInt64Array(value: unknown): value is BigInt64Array;
/**
* Check if value is BigUint64Array
* @param value - Value to check
* @returns True if value is BigUint64Array
*/
function isBigUint64Array(value: unknown): value is BigUint64Array;Usage Examples:
import is from '@sindresorhus/is';
// BigInt64Array: signed 64-bit integers
const bigInt64 = new BigInt64Array([123n, -456n]);
is.bigInt64Array(bigInt64); // => true
// BigUint64Array: unsigned 64-bit integers
const bigUint64 = new BigUint64Array([123n, 456n]);
is.bigUint64Array(bigUint64); // => true
// Working with large integers
function processLargeNumbers(data: unknown) {
if (is.bigInt64Array(data)) {
// data is now typed as BigInt64Array
for (const bigNum of data) {
console.log('Large number:', bigNum.toString());
}
}
}Type checking for buffer objects that typed arrays are built on.
/**
* Check if value is ArrayBuffer
* @param value - Value to check
* @returns True if value is ArrayBuffer
*/
function isArrayBuffer(value: unknown): value is ArrayBuffer;
/**
* Check if value is SharedArrayBuffer
* @param value - Value to check
* @returns True if value is SharedArrayBuffer
*/
function isSharedArrayBuffer(value: unknown): value is SharedArrayBuffer;
/**
* Check if value is DataView
* @param value - Value to check
* @returns True if value is DataView
*/
function isDataView(value: unknown): value is DataView;Usage Examples:
import is from '@sindresorhus/is';
// ArrayBuffer: raw binary data buffer
const buffer = new ArrayBuffer(16);
is.arrayBuffer(buffer); // => true
// SharedArrayBuffer: shared between workers
const sharedBuffer = new SharedArrayBuffer(1024);
is.sharedArrayBuffer(sharedBuffer); // => true
// DataView: flexible view of buffer
const view = new DataView(buffer);
is.dataView(view); // => true
// Working with buffers
function processBuffer(data: unknown) {
if (is.arrayBuffer(data)) {
// data is now typed as ArrayBuffer
console.log('Buffer size:', data.byteLength);
// Create typed array views
const uint8View = new Uint8Array(data);
const uint32View = new Uint32Array(data);
console.log('As Uint8Array length:', uint8View.length);
console.log('As Uint32Array length:', uint32View.length);
}
}
function processDataView(view: unknown) {
if (is.dataView(view)) {
// view is now typed as DataView
const int32 = view.getInt32(0, true); // little endian
const float64 = view.getFloat64(8, false); // big endian
console.log('Int32:', int32, 'Float64:', float64);
}
}Check if a value is a Node.js Buffer (with deprecation note).
/**
* Check if value is Node.js Buffer
* Note: Prefer using Uint8Array instead of Buffer
* @param value - Value to check
* @returns True if value is Buffer
*/
function isBuffer(value: unknown): value is NodeBuffer;
type NodeBuffer = Buffer; // Node.js Buffer typeUsage Examples:
import is from '@sindresorhus/is';
// Node.js only
const buffer = Buffer.from('hello');
is.buffer(buffer); // => true
const uint8Array = new Uint8Array([1, 2, 3]);
is.buffer(uint8Array); // => false
// Migration pattern
function processData(data: unknown) {
if (is.buffer(data)) {
// Convert Buffer to Uint8Array (recommended)
const uint8Array = new Uint8Array(data);
return uint8Array;
} else if (is.uint8Array(data)) {
return data;
}
throw new Error('Expected Buffer or Uint8Array');
}import is from '@sindresorhus/is';
function processBinaryData(data: unknown) {
if (is.uint8Array(data)) {
// Process as byte array
for (let i = 0; i < data.length; i++) {
console.log(`Byte ${i}: ${data[i]}`);
}
} else if (is.float32Array(data)) {
// Process as floating point data
const max = Math.max(...data);
const min = Math.min(...data);
console.log(`Range: ${min} to ${max}`);
} else if (is.arrayBuffer(data)) {
// Create appropriate view
const view = new Uint8Array(data);
processBinaryData(view);
}
}import is from '@sindresorhus/is';
function processImageData(data: unknown) {
if (is.uint8ClampedArray(data)) {
// Canvas ImageData uses Uint8ClampedArray
for (let i = 0; i < data.length; i += 4) {
const r = data[i];
const g = data[i + 1];
const b = data[i + 2];
const a = data[i + 3];
// Process RGBA pixel
const gray = Math.round(0.299 * r + 0.587 * g + 0.114 * b);
data[i] = data[i + 1] = data[i + 2] = gray;
}
}
}length, byteLength, buffer, byteOffsetUint8ClampedArray automatically clamps values to 0-255 rangebigint primitives, not regular numbersSharedArrayBuffer enables sharing memory between web workersDataView provides flexible, endian-aware access to ArrayBuffer dataBuffer is being deprecated in favor of Uint8ArrayInstall with Tessl CLI
npx tessl i tessl/npm-sindresorhus--is