microsoft / microsoft/TypeScript

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

Open
#61,835 1 comment 1 reaction 0 assignees View on GitHub

Nobody has claimed this yet.

Domain: lib.d.ts Help Wanted Possible Improvement
Dominant language
Go
Stars
111k
Forks
14.3k
Avg merge
2d 4h
Merged PRs (30d)
132

Description

### 🔎 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.

Contributor guide

Open the contributing guide

First steps

  1. Read the whole issue, then the project's contributing guide.
  2. Comment on the issue to say you are picking it up — it saves two people doing the same work.
  3. Fork the repository and make your change on a branch.
  4. Open a pull request that references the issue number.

Research direction

Start with the linked TypeScript Playground reproduction and inspect the library declaration for Function.apply, focusing on the second parameter type. Confirm that the declaration accepts the Uint8Array example without error while preserving the existing apply behavior.

Written by the indexing model from the issue text.

Assessment

Tech stack
typescript
Domain
compilers
Issue type
Bug
Difficulty
2/5
Estimated time
1-3 hours
Activity status
Stale
Clarity
Mostly clear
Newbie friendliness
48/100

Get new issues in your inbox

A short digest of beginner-friendly GitHub issues.