Skip to content

Comments

Restrict types from Uint8Array<ArrayBufferLike> to Uint8Array<ArrayBuffer>#133

Draft
ussu wants to merge 1 commit intocantoo-scribe:masterfrom
ussu:ussu/u-int-8-array-array-buffer
Draft

Restrict types from Uint8Array<ArrayBufferLike> to Uint8Array<ArrayBuffer>#133
ussu wants to merge 1 commit intocantoo-scribe:masterfrom
ussu:ussu/u-int-8-array-array-buffer

Conversation

@ussu
Copy link

@ussu ussu commented Jan 26, 2026

What?

In Typescript 5.9, there is a more precise typechecking, for instance regarding ArrayBuffer vs SharedArrayBuffer, which are both an ArrayBufferLike
It seems like what pdf-lib uses can always be typed as an ArrayBuffer rather than a SharedArrayBuffer or ArrayBufferLike, so this PR aims to narrow the return types in the package to ArrayBuffer to improve compatibility with other libs

Why?

#132

How?

Updating @types/node to a recent version allows typescript to narrow down most instances of Uint8Array to Uint8Array
In functions exposed by pdf-lib, simply removing the return types explicitly set to Uint8Array ( = Uint8Array) allows typescript to correctly infer the return type to Uint8Array

Testing?

New Dependencies?

no

Screenshots

n/a

Suggested Reading?

Anything Else?

Checklist

  • I read CONTRIBUTING.md.
  • I read MAINTAINERSHIP.md#pull-requests.
  • I added/updated unit tests for my changes.
  • I added/updated integration tests for my changes.
  • I ran the integration tests.
  • I tested my changes in Node ⌛ starts well but fails at test12
  • I tested my changes in Deno ❌ did not manage to run tests with Deno
  • I tested my changes in the browser ✅ everything works fine
  • I viewed documents produced with my changes in Adobe Acrobat, Foxit Reader, Firefox, and Chrome.
  • I added/updated doc comments for any new/modified public APIs.
  • My changes work for both new and existing PDF files.
  • I ran the linter on my changes.

@ussu ussu changed the title Restrict types from Uint8Array<ArrayBufferLike> to Uint8Array<ArrayBu… Restrict types from Uint8Array<ArrayBufferLike> to Uint8Array<ArrayBuffer> Jan 29, 2026
@ussu ussu force-pushed the ussu/u-int-8-array-array-buffer branch from e4509d7 to 6ae67fe Compare January 29, 2026 23:47
@@ -1,83 +1,84 @@
import {
parse as parseHtml,
Copy link
Collaborator

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

What is this import rewriting? Can you revert and present only the needed changes?


import CustomFontEmbedder from './CustomFontEmbedder';
import PDFHexString from '../objects/PDFHexString';
import { Cache, mergeUint8Arrays, toHexStringOfMinLength } from '../../utils';
Copy link
Collaborator

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Please leave the imports order alone ^^"

"compilerOptions": {
/* Basic Options */
"target": "es2015",
"target": "es2024",
Copy link
Collaborator

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

This is not a trivial change. Let's keep the original target which provides a wider support.

@Sharcoux
Copy link
Collaborator

Thanks for your contribution.

Can you just fix your MR to limit to what is strictly needed to achieve the goal of the PR please? I'll merge after that.

Thanks.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Projects

None yet

Development

Successfully merging this pull request may close these issues.

2 participants