microsoft / microsoft/TypeScript

The second parameter type of `apply` should be `ArrayLike<T>` instead of `T[]`

未关闭
#61,835 1 条评论 1 个 reaction 已指派 0 人 在 GitHub 查看
Domain: lib.d.ts Help Wanted Possible Improvement
主要语言
Go
星标
111k
派生
14.3k
平均合并
2 天 4 小时
30 天内合并 PR
132

描述

### 🔎 Search Terms

String.prototype.apply

I found #33173, but the issue was closed without a reason. The problem still persists.

### 🕗 Version & Regression Information

At least since v3.3.3 according to the Playground.

### ⏯ Playground Link

https://www.typescriptlang.org/play/?#code/MYewdgzgLgBArgDhgXhmApgdxgVQJZhQICCATqQIYCeAFANoAMAHgCwCMANMywExesBmALoBKANwAoAMpRSBAOYA6AGakQAWwDCACwqlNIACbpFFAA5mANrTBxLljvATigA

### 💻 Code

```ts
const u8 = new Uint8Array([0x41,0x42,0x43]);
String.fromCharCode.apply(null, u8);
```

### 🙁 Actual behavior

```
Argument of type 'Uint8Array' is not assignable to parameter of type 'number[]'.
Type 'Uint8Array' is missing the following properties from type 'number[]': pop, push, concat, shift, and 3 more.(2345)
```

### 🙂 Expected behavior

no error

### Additional information about the issue

This code works fine at runtime. [ECMAScript spec](https://262.ecma-international.org/#sec-function.prototype.apply) does not require a genuine array at all. ArrayLike is sufficient.

Workarounds such as
```ts
String.fromCharCode.apply(null, Array.from(u8));
```
will impose runtime overheads.

贡献指南

打开贡献指南

调研方向

从链接的 TypeScript Playground 复现开始,检查 Function.apply 的库声明,重点关注第二个参数的类型。确认该声明能够无错误地接受 Uint8Array 示例,同时保留现有的 apply 行为。

由索引模型根据 Issue 内容生成。

评估

技术栈
typescript
领域
compilers
Issue 类型
缺陷
难度
2/5
预计耗时
1-3 小时
活跃度
停滞
描述清晰度
基本清楚
新手友好度
48/100

把新 issue 发到你的邮箱

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