microsoft / microsoft/TypeScript

Split TypedArray overload into separate overloads

Open
#42,354 4 comments 1 reaction 0 assignees View on GitHub

Nobody has claimed this yet.

Awaiting More Feedback Suggestion
Dominant language
Go
Stars
111k
Forks
14.3k
Avg merge
2d 4h
Merged PRs (30d)
132

Description

lib Update Request

Configuration Check

My compilation target is ESNext and my lib is the default.

Missing / Incorrect Definition

All TypedArrays have five overloads, one of which is the following:

TypedArray(buffer: ArrayBuffer, byteOffset?: number, length?: number);

I believe that this should be split into three separate overloads:

new TypedArray(buffer: ArrayBuffer);
new TypedArray(buffer: ArrayBuffer, byteOffset: number);
new TypedArray(buffer: ArrayBuffer, byteOffset: number, length: number);

or

new TypedArray(buffer: ArrayBuffer, byteOffset?: number);
new TypedArray(buffer: ArrayBuffer, byteOffset: number, length: number);

Sample Code

I came across something vaguely like this:

declare function getOffset(): undefined;

const offset = getOffset();

new Uint8Array(new ArrayBuffer(0), offset, 0);

and thought that their code definitely had a bug, but, it turns out that undefined-ish values were allowed for the second parameter, because the type was of number | undefined.

This would break code, but I believe that this will break little to no code in practice.

Documentation Link

N/A: no new additions

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 at the TypedArray constructor declarations described in the issue and reproduce the sample where an undefined offset is accepted. Compare the existing overloads with the proposed separate arities, then verify that valid constructor calls still type-check while the three-argument call with an undefined offset is rejected.

Written by the indexing model from the issue text.

Assessment

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

Get new issues in your inbox

A short digest of beginner-friendly GitHub issues.