Aller au contenu principal

isTypedArray()

isTypedArray(value): value is TypedArray

Checks if value is a TypedArray.

DEPRECATED

Use ArrayBuffer.isView() directly instead. Note: ArrayBuffer.isView() also returns true for DataView.


Parameters

value: unknown

The value to check.


Returns: value is TypedArray

true if value is a TypedArray, else false.


See Also


Since

2.0.0


Also known as

isTypedArray (Lodash, es-toolkit) · ❌ (Remeda, Radashi, Ramda, Effect, Modern Dash, Antfu)


Example

// ❌ Deprecated approach
isTypedArray(new Uint8Array(2)); // => true
isTypedArray(new Float32Array()); // => true
isTypedArray([1, 2, 3]); // => false

// ✅ Recommended approach
ArrayBuffer.isView(new Uint8Array(2)); // => true
ArrayBuffer.isView(new Float32Array()); // => true
ArrayBuffer.isView([1, 2, 3]); // => false

// Note: ArrayBuffer.isView also matches DataView
ArrayBuffer.isView(new DataView(new ArrayBuffer(2))); // => true

How it works?

Checks if value is a typed array. Deprecated: Use ArrayBuffer.isView() or instanceof checks.

Native Equivalent

// ❌ isTypedArray(value)
// ✅ ArrayBuffer.isView(value) && !(value instanceof DataView)

Use Cases

Check typed array 📌

Check if value is a TypedArray.

ArrayBuffer.isView(new Uint8Array(2));   // true
ArrayBuffer.isView(new Float32Array()); // true
ArrayBuffer.isView([1, 2, 3]); // false

Handle binary data

Detect binary data for special handling.

function processData(data: unknown) {
if (ArrayBuffer.isView(data)) {
return processBinary(data);
}
return processJSON(data);
}