Skip to content

Constructor for DataView is too broad #52815

Closed
@ngbrown

Description

@ngbrown

lib Update Request

Configuration Check

My compilation target is ES2021 and my lib is ["DOM", "DOM.Iterable", "ES2021"]. TypeScript version is 4.9.5.

Missing / Incorrect Definition

The DataView constructor parameter type is incorrectly broad by accepting ArrayBufferLike because TypedArray's such as Uint8Array match the ArrayBufferLike type but are not valid parameters. I think the DataView constructor parameter should only accept ArrayBuffer | SharedArrayBuffer.

Sample Code

The following produces no error in TypeScript but does produce an error in Node 16, Chrome 109, and FireFox 111b1:

new DataView(new Uint8Array(32));

The runtime error is:

Uncaught TypeError: First argument to DataView constructor must be an ArrayBuffer
    at new DataView (<anonymous>)

The correct code that does run is:

new DataView(new Uint8Array(32).buffer)

Documentation Link

Metadata

Metadata

Assignees

No one assigned

    Labels

    BugA bug in TypeScriptDomain: lib.d.tsThe issue relates to the different libraries shipped with TypeScriptHelp WantedYou can do this

    Type

    No type

    Projects

    No projects

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions