-
Notifications
You must be signed in to change notification settings - Fork 12.6k
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Constructor for DataView is too broad #52815
Comments
const foo: ArrayBuffer = new Uint8Array(42); // no error |
I knew that to a degree (duck typing and all), and I suppose someone will declare this as duplicate of #202, which I didn't know about. But this seems like these types should be the exception to structural typing because JavaScript is much more strict about the types actually being With |
Yeah, I think there's a feature request somewhere to be able to expand types in function hints. Apparently this is intentional because doing otherwise is a breaking change: #31311 (comment) |
Interestingly, interface DataViewConstructor {
readonly prototype: DataView;
new<T extends (ArrayBuffer | SharedArrayBuffer) = ArrayBufferLike>(buffer: T & Exclude<T, {BYTES_PER_ELEMENT: number}>, byteOffset?: number, byteLength?: number): DataView;
}
declare var DataView: DataViewConstructor; But it is a breaking change for types that inherit from |
I think this is actually a lot easier to accomplish: new(buffer: ArrayBufferLike & { BYTES_PER_ELEMENT?: never }, byteOffset?: number, byteLength?: number): DataView; |
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 acceptingArrayBufferLike
becauseTypedArray
's such asUint8Array
match theArrayBufferLike
type but are not valid parameters. I think theDataView
constructor parameter should only acceptArrayBuffer | SharedArrayBuffer
.Sample Code
The following produces no error in TypeScript but does produce an error in Node 16, Chrome 109, and FireFox 111b1:
The runtime error is:
The correct code that does run is:
Documentation Link
TypeError
is to be thrown if this is a detached buffer, i.e. without array buffer data)The text was updated successfully, but these errors were encountered: