Skip to content

Update TypedArray constructors to disallow invalid (typedArray, byteOffset, byteLength) #44191

Closed

Description

@tjjfvi

lib Update Request

Configuration Check

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

Missing / Incorrect Definition

TypedArray constructors allows passing (TypedArray, byteOffset, byteLength), when only byteOffset and byteLength are only used when passing in an ArrayBuffer

Sample Code

const buf = new ArrayBuffer(1024);
const uint8 = new Uint8Array(buf, 16, 128)
// No TS error, even though the `16` and `32` are completely ignored
const subUint8 = new Uint8Array(uint8, 16, 32)
subUint8.byteLength // Oops, this is 128, not 32.

Documentation Link

MDN on Uint8Array constructor

new Uint8Array(); // new in ES2017
new Uint8Array(length);
new Uint8Array(typedArray);
new Uint8Array(object);

new Uint8Array(buffer);
new Uint8Array(buffer, byteOffset);
new Uint8Array(buffer, byteOffset, length);

Activity

  1. RyanCavanaugh commented on Sep 7, 2026

    @RyanCavanaugh
    Member

    The constructor call from the report is now rejected. TypeScript 6.0.3 and current native TypeScript 7.1.0-dev.20260906.1 report TS2769 because the overload with byteOffset and length requires an ArrayBuffer or ArrayBufferLike as its first argument; another typed array does not match that overload.

    TypeScript 4.3.2 accepted the call, so its offset and length arguments could be ignored at runtime.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Metadata

Metadata

Assignees

No one assigned

    Labels

    BugA bug in TypeScriptDomain: lib.d.tsThe issue relates to the different libraries shipped with TypeScriptFixedA PR has been merged for this issueHelp WantedYou can do this

    Type

    No type

    Projects

    No projects

      Relationships

      None yet

      Development

      No branches or pull requests

      Issue actions