isTypedArray
Checks whether a value is a TypedArray. This includes all built‑in typed array types such as Uint8Array, Float32Array, and BigInt64Array. You may optionally enforce a specific TypedArray constructor.
This helper never throws and never mutates input.
Signature
function isTypedArray<T extends TypedArray = TypedArray>(
value: unknown,
expected?: { new (...args: unknown[]): T },
): value is T
Parameters
| Name | Data type | Description |
|---|---|---|
| value | unknown |
The value to check. Must be a TypedArray to return true. |
| expected | constructor |
Optional. If provided, the value must be an instance of this specific TypedArray type. |
Returns
A boolean:
true: If the value is aTypedArray.true: If the value is aTypedArrayand matches the expected type.false: Otherwise.
Examples
isTypedArray(new Uint8Array()) // true
isTypedArray(new Float32Array()) // true
isTypedArray(new Uint8Array(), Uint8Array) // true
isTypedArray(new Uint8Array(), Float32Array) // false
isTypedArray([]) // false
isTypedArray(new DataView(...)) // false
isTypedArray(null) // false
Notes
- This helper uses
ArrayBuffer.isViewand excludesDataView, matching the ECMAScript definition ofTypedArray. - It accepts
TypedArraysubclasses and wrapper objects. - Use
validateTypedArrayif you need aResult<T>instead of a boolean.