microsoft / microsoft/TypeScript

`IntegerTypedArray` type required for use with `crypto.getRandomValues`

未关闭
#61,768 2 条评论 2 个 reaction 已指派 3 人 在 GitHub 查看

@RyanCavanaugh 已经在做这个了。

开始于 2026年9月19日。

  • #64343 来自 @copilot-swe-agent —— 未关闭
Bug Domain: lib.d.ts Needs Human Review
主要语言
Go
星标
111k
派生
14.3k
平均合并
2 天 4 小时
30 天内合并 PR
132

描述

### ⚙ Compilation target

any

### ⚙ Library

latest

### Missing / Incorrect Definition

The current DOM types `` [are wrong](https://github.com/microsoft/TypeScript-DOM-lib-generator/issues/1194) for `crypto.getRandomValues`. `null` is never accepted, and `ArrayBufferView` is too broad as float arrays also aren't accepted.

However, [fixing](https://github.com/microsoft/TypeScript-DOM-lib-generator/pull/2028) 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`:

```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`:

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

### Sample Code

```TypeScript
// 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[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'.(2339)
values!.BYTES_PER_ELEMENT
```

### Documentation Link

[10.1.1 The getRandomValues method - Web Cryptography Level 2](https://www.w3.org/TR/webcrypto/#dfn-Crypto-method-getRandomValues)

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

贡献指南

打开贡献指南

从这里开始

  1. 先读完整个 Issue,再读项目的贡献指南。
  2. 在 Issue 下留言说明你要接手 —— 这能避免两个人做同样的事。
  3. Fork 仓库,在一个分支上完成修改。
  4. 提交 Pull Request,并在描述里引用这个 Issue 编号。

评估

这个 Issue 还没有评估数据。

把新 issue 发到你的邮箱

精选适合新手参与的 GitHub issue 摘要。