microsoft / microsoft/TypeScript

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

Offen
#61,835 1 Kommentar 1 Reaktion 0 zugewiesene Personen Auf GitHub ansehen
Domain: lib.d.ts Help Wanted Possible Improvement
Vorherrschende Sprache
Go
Sterne
111k
Forks
14.3k
Ø Merge
2 T. 4 Std.
Gemergte PRs (30 T.)
132

Beschreibung

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

Beitragsleitfaden

Beitragsleitfaden öffnen

Rechercherichtung

Beginne mit der verlinkten TypeScript Playground-Reproduktion und prüfe die Bibliotheksdeklaration für Function.apply, wobei du dich auf den Typ des zweiten Parameters konzentrierst. Bestätige, dass die Deklaration das Uint8Array-Beispiel ohne Fehler akzeptiert und dabei das bestehende apply-Verhalten beibehält.

Vom Indexierungsmodell aus dem Issue-Text verfasst.

Bewertung

Tech-Stack
typescript
Bereich
compilers
Issue-Typ
Bug
Schwierigkeit
2/5
Geschätzter Aufwand
1-3 Stunden
Aktivitätsstatus
Veraltet
Klarheit
Größtenteils klar
Anfängerfreundlichkeit
48/100

Neue Issues direkt in Ihr Postfach

Eine kurze Übersicht über anfängerfreundliche GitHub-Issues.