sindresorhus/is · error · TypeError
Expected value which is `Int16Array`, received value of type
Error message
Expected value which is `Int16Array`, received value of type `${is(value)}`. What it means
Thrown by `assertInt16Array()` when `isInt16Array()` fails — the value is not a genuine Int16Array instance. The check is by internal tag, so structurally similar values (plain arrays, other typed-array element types) are rejected.
Source
Thrown at source/index.ts:1677
throw new TypeError(message ?? typeErrorMessage('HTMLElement', value));
}
}
export function assertInfinite(value: unknown, message?: string): asserts value is number {
if (!isInfinite(value)) {
throw new TypeError(message ?? typeErrorMessage('infinite number', value));
}
}
export function assertInRange(value: number, range: number | [number, number], message?: string): asserts value is number {
if (!isInRange(value, range)) {
throw new TypeError(message ?? typeErrorMessage('in range', value));
}
}
export function assertInt16Array(value: unknown, message?: string): asserts value is Int16Array {
if (!isInt16Array(value)) {
throw new TypeError(message ?? typeErrorMessage('Int16Array', value));
}
}
export function assertInt32Array(value: unknown, message?: string): asserts value is Int32Array {
if (!isInt32Array(value)) {
throw new TypeError(message ?? typeErrorMessage('Int32Array', value));
}
}
export function assertInt8Array(value: unknown, message?: string): asserts value is Int8Array {
if (!isInt8Array(value)) {
throw new TypeError(message ?? typeErrorMessage('Int8Array', value));
}
}
export function assertInteger(value: unknown, message?: string): asserts value is number {
if (!isInteger(value)) {
throw new TypeError(message ?? typeErrorMessage('integer', value));View on GitHub ↗ (pinned to 7821031c66)
Solutions
- Wrap the source in the right view: `new Int16Array(arrayBuffer)` or `Int16Array.from(plainArray)`.
- Double-check signedness — if the producer emits Uint16Array, either reinterpret the buffer (`new Int16Array(u16.buffer)`) or change the assertion to match the actual type.
- For Node Buffers, create a view over the underlying memory: `new Int16Array(buf.buffer, buf.byteOffset, buf.byteLength / 2)`.
Example fix
// before
ws.onmessage = e => { assertInt16Array(e.data); }; // ArrayBuffer
// after
ws.onmessage = e => {
const samples = new Int16Array(e.data);
assertInt16Array(samples);
}; Defensive patterns
Strategy: type-guard
Validate before calling
if (!(value instanceof Int16Array)) {
throw new TypeError(`Expected Int16Array, got ${typeof value}`);
} Type guard
function isInt16Array(value: unknown): value is Int16Array {
return value instanceof Int16Array;
} Try / catch
try {
assert.int16Array(value);
} catch (error) {
if (error instanceof TypeError) { /* convert with Int16Array.from or reject */ }
else throw error;
} Prevention
- Convert explicitly with `Int16Array.from(arr)` or `new Int16Array(buffer)` — plain arrays and other typed-array kinds fail
- Uint16Array and Int16Array are distinct despite identical element size; keep signedness straight in signatures
- Use the library's cross-realm-safe `is.int16Array` when values may come from workers or iframes
When it happens
Trigger: `is.assert.int16Array(value)` with a Uint16Array, Int8Array, plain `number[]`, ArrayBuffer, DataView, or Node Buffer.
Common situations: Audio/binary-protocol code confusing signed and unsigned 16-bit views (Int16Array vs Uint16Array — a very common mix-up for PCM audio); receiving raw ArrayBuffers from WebSocket/fetch and forgetting to wrap them; serialized data arriving as plain arrays.
Related errors
- Expected value which is `BigInt64Array`, received value of t
- Expected value which is `BigUint64Array`, received value of
- Expected value which is `DataView`, received value of type `
- Expected value which is `Float32Array`, received value of ty
- Expected value which is `Float64Array`, received value of ty
AI-assisted analysis of sindresorhus/is@7821031c66 (2026-07-31).
Data as JSON: /data/errors/a522f7fad154ada5.json.
Report an issue: GitHub ↗.