Skip to content

IntegerTypedArray type required for use with crypto.getRandomValues #61768

Description

@lionel-rowe

⚙ Compilation target

any

⚙ Library

latest

Missing / Incorrect Definition

The current DOM types <T extends ArrayBufferView | null> are wrong for crypto.getRandomValues. null is never accepted, and ArrayBufferView is too broad as float arrays also aren't accepted.

However, fixing in TypeScript-DOM-lib-generator requires conditional inclusion of BigInt64Array | BigUint64Array depending on ES2020 availability. I don't think there's a way to fix that within TypeScript-DOM-lib-generator without this upstream change in TypeScript.

es5.d.ts:

interface IntegerTypedArrayTypes {
    Int8Array: Int8Array;
    Int16Array: Int16Array;
    Int32Array: Int32Array;
    Uint8Array: Uint8Array;
    Uint16Array: Uint16Array;
    Uint32Array: Uint32Array;
    Uint8ClampedArray: Uint8ClampedArray;
}

type IntegerTypedArray = IntegerTypedArrayTypes[keyof IntegerTypedArrayTypes];

es2020.bigint.d.ts:

interface IntegerTypedArrayTypes {
    BigInt64Array: BigInt64Array;
    BigUint64Array: BigUint64Array;
}

Sample Code

// Unused '@ts-expect-error' directive.(2578)
// @ts-expect-error
crypto.getRandomValues(null)
// Unused '@ts-expect-error' directive.(2578)
// @ts-expect-error
crypto.getRandomValues(new Float64Array())

declare const arr: Parameters<typeof crypto.getRandomValues>[0]
const values = crypto.getRandomValues(arr)

// 'values' is possibly 'null'.(18047)
values.BYTES_PER_ELEMENT

// Property 'BYTES_PER_ELEMENT' does not exist on type 'ArrayBufferView<ArrayBufferLike>'.(2339)
values!.BYTES_PER_ELEMENT

Documentation Link

10.1.1 The getRandomValues method - Web Cryptography Level 2

If array is not an Int8Array, Uint8Array, Uint8ClampedArray, Int16Array, Uint16Array, Int32Array, Uint32Array, BigInt64Array, or BigUint64Array, then throw a TypeMismatchError and terminate the algorithm.

Activity

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

Metadata

Metadata

Labels

BugA bug in TypeScriptDomain: lib.d.tsThe issue relates to the different libraries shipped with TypeScript

Type

No type

Projects

No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions