Skip to content

Add IntegerTypedArray type and use it for crypto.getRandomValues - #64343

Draft
Ryan Cavanaugh (RyanCavanaugh) with Copilot wants to merge 2 commits into
mainfrom
copilot/integer-typed-array-fix
Draft

Ryan Cavanaugh (RyanCavanaugh) with Copilot wants to merge 2 commits into
mainfrom
copilot/integer-typed-array-fix

Conversation

Copilot AI commented Sep 19, 2026

Copy link
Copy Markdown
Contributor

crypto.getRandomValues accepted any ArrayBufferView (including float arrays, which browsers reject with TypeMismatchError), and its constraint-instantiated parameter type carried no typed-array members. Expressing the correct constraint requires a lib-level type, since BigInt64Array/BigUint64Array only exist under ES2020.

Analysis

The DOM signature was getRandomValues<T extends Exclude<BufferSource, ArrayBuffer>>(array: T): T. Two consequences:

  • crypto.getRandomValues(new Float64Array(1)) type-checks, but throws at runtime.
  • Parameters<typeof crypto.getRandomValues>[0] resolves to ArrayBufferView, so crypto.getRandomValues(array).BYTES_PER_ELEMENT errors with TS2339.

Per WebCrypto §10.1.1, only the nine integer typed arrays are valid. Since the bigint views are gated on lib selection, the union has to be assembled via interface merging in the language libs rather than in TypeScript-DOM-lib-generator.

Fix

  • lib.es5.d.ts: adds IntegerTypedArrayTypes and type IntegerTypedArray = IntegerTypedArrayTypes[keyof IntegerTypedArrayTypes], following the existing WeakKey/WeakKeyTypes pattern. Members are declared over ArrayBufferLike (rather than relying on the ArrayBuffer default) so SharedArrayBuffer-backed views and Uint8Array<ArrayBufferLike> values such as Node's Buffer keep working.
  • lib.es2020.bigint.d.ts: merges BigInt64Array/BigUint64Array into IntegerTypedArrayTypes, so they only join the union when the ES2020 bigint lib is in play.
  • lib.dom.d.ts / lib.webworker.d.ts: constraint changed to getRandomValues<T extends IntegerTypedArray>(array: T): T. These files are generated; the corresponding generator change belongs upstream in TypeScript-DOM-lib-generator.
  • tsc/internal/fourslash/tests/util/util.go: adds the two new globals to the shared expected global-completion list.
  • tsc/testdata/tests/cases/compiler/integerTypedArray.ts + baselines cover acceptance, rejection, and the Parameters<...> round trip.
crypto.getRandomValues(new Uint8Array(1));    // ok
crypto.getRandomValues(new BigInt64Array(1)); // ok under es2020+
crypto.getRandomValues(new Float64Array(1));  // TS2345
crypto.getRandomValues(null);                 // TS2345

declare const array: Parameters<typeof crypto.getRandomValues>[0]; // IntegerTypedArray
crypto.getRandomValues(array).BYTES_PER_ELEMENT;                   // number

Copilot Checklist

I successfully ran the applicable command at the end of my session, and it completed without error:

  • npx hereby validate
  • npx hereby validate --api (for TypeScript API changes)

Co-authored-by: RyanCavanaugh <6685088+RyanCavanaugh@users.noreply.github.com>
Copilot AI changed the title [WIP] Fix IntegerTypedArray type for crypto.getRandomValues Add IntegerTypedArray type and use it for crypto.getRandomValues Sep 19, 2026
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

For Uncommitted Bug PR for untriaged, rejected, closed or missing bug

Projects

None yet

Development

Successfully merging this pull request may close these issues.

IntegerTypedArray type required for use with crypto.getRandomValues

2 participants