Skip to content

The second parameter type of apply should be ArrayLike<T> instead of T[]Β #61835

Open
@vyv03354

Description

@vyv03354

πŸ”Ž 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.

Metadata

Metadata

Assignees

No one assigned

    Labels

    Help WantedYou can do thisPossible ImprovementThe current behavior isn't wrong, but it's possible to see that it might be better in some cases

    Type

    No type

    Projects

    No projects

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions