Microsoft/TypeScript

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

Open

#61,835 创建于 2025年6月9日

在 GitHub 查看
 (1 评论) (1 反应) (0 负责人)TypeScript (6,726 fork)batch import
Domain: lib.d.tsHelp WantedPossible Improvement

仓库指标

Star
 (48,455 star)
PR 合并指标
 (平均合并 6天 17小时) (30 天内合并 9 个 PR)

描述

🔎 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

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

🙁 Actual behavior

Argument of type 'Uint8Array<ArrayBuffer>' is not assignable to parameter of type 'number[]'.
  Type 'Uint8Array<ArrayBuffer>' 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 does not require a genuine array at all. ArrayLike is sufficient.

Workarounds such as

String.fromCharCode.apply(null, Array.from(u8));

will impose runtime overheads.

贡献者指南