Skip to content

Instantly share code, notes, and snippets.

@patte
Last active September 23, 2025 13:56
Show Gist options
  • Save patte/9b5c35ce0db897b11a7f7d039d0c5f38 to your computer and use it in GitHub Desktop.
Save patte/9b5c35ce0db897b11a7f7d039d0c5f38 to your computer and use it in GitHub Desktop.
fix: Argument of type 'Uint8Array<ArrayBufferLike>' is not assignable to parameter of type 'Uint8Array<ArrayBuffer>'
/**
* Asserts that the provided Uint8Array is backed by an ArrayBuffer.
*
* Throws an error if the buffer is a SharedArrayBuffer or any other type.
* This is useful for ensuring (type) compatibility with APIs that require Uint8Array<ArrayBuffer>.
* Usage:
* // bytes: Uint8Array<ArrayBufferLike>
* assertArrayBuffer(bytes);
* // bytes: Uint8Array<ArrayBuffer>
*
* Fixes: Argument of type 'Uint8Array<ArrayBufferLike>' is not assignable to parameter of type 'Uint8Array<ArrayBuffer>'.
* See: https://www.typescriptlang.org/docs/handbook/release-notes/typescript-5-7.html#support-for---target-es2024-and---lib-es2024
*
* @param u8 - The Uint8Array to check.
* @throws {Error} If the buffer is a SharedArrayBuffer or not an ArrayBuffer.
*/
export function assertArrayBuffer(
u8: Uint8Array<ArrayBufferLike>,
): asserts u8 is Uint8Array<ArrayBuffer> {
if (u8.buffer instanceof ArrayBuffer) {
return;
}
if (typeof SharedArrayBuffer !== 'undefined' && u8.buffer instanceof SharedArrayBuffer) {
throw new Error('SharedArrayBuffer not supported');
}
throw new Error('Expected Uint8Array with ArrayBuffer');
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment