Closed
Description
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
- https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Global_Objects/DataView/DataView
- https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Global_Objects/TypedArray/buffer
- https://tc39.es/ecma262/multipage/structured-data.html#sec-dataview-constructor (specifically a
TypeError
is to be thrown if this is a detached buffer, i.e. without array buffer data)