-
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
Uint8Array assignable to ArrayBuffer despite having subtle differences leading to browser TypeErrors #42534
Comments
β¦on key-id Since at least the v3.21.1 release, we had an issue that appeared when blacklisting Representation based on the keyID (e.g. when a `keystatuseschange` event indicates us that we are "output-restricted and when fallbacking is enabled through the related `keySystems` options). This issue prevented from fallbacking, it just threw an Error message along the lines of: `TypeError: DataView: expected ArrayBuffer, got Uint8Array` The root cause was linked to typing issues (we consequently cleverly redirect all wrong-doing to typescript, not our fault at all here :p: microsoft/TypeScript#42534) where an Uint8Array was considered to be a valid ArrayBuffer. At some point, a `new DataView(whatIThoughtToBeAnArrayBufferWhichIsInRealityAUint8Array)` was done, leading to the aforementioned error. The fix here is to now consider the type to be an Uint8Array and to do Uint8Array things instead (i.e. no DataView anymore).
β¦on key-id Since at least the v3.21.1 release, we had an issue that appeared when blacklisting Representation based on the keyID (e.g. when a `keystatuseschange` event indicates us that we are "output-restricted and when fallbacking is enabled through the related `keySystems` options). This issue prevented from fallbacking, it just threw an Error message along the lines of: `TypeError: DataView: expected ArrayBuffer, got Uint8Array` The root cause was linked to typing issues (we consequently cleverly redirect all wrong-doing to typescript, not our fault at all here :p: microsoft/TypeScript#42534) where an Uint8Array was considered to be a valid ArrayBuffer. At some point, a `new DataView(whatIThoughtToBeAnArrayBufferWhichIsInRealityAUint8Array)` was done, leading to the aforementioned error. The fix here is to now consider the type to be an Uint8Array and to do Uint8Array things instead (i.e. no DataView anymore).
β¦on key-id Since at least the v3.21.1 release, we had an issue that appeared when blacklisting Representation based on the keyID (e.g. when a `keystatuseschange` event indicates us that we are "output-restricted and when fallbacking is enabled through the related `keySystems` options). This issue prevented from fallbacking, it just threw an Error message along the lines of: `TypeError: DataView: expected ArrayBuffer, got Uint8Array` The root cause was linked to typing issues (we consequently cleverly redirect all wrong-doing to typescript, not our fault at all here :p: microsoft/TypeScript#42534) where an Uint8Array was considered to be a valid ArrayBuffer. At some point, a `new DataView(whatIThoughtToBeAnArrayBufferWhichIsInRealityAUint8Array)` was done, leading to the aforementioned error. The fix here is to now consider the type to be an Uint8Array and to do Uint8Array things instead (i.e. no DataView anymore).
As mentioned in #31311, this is a duplicate of #202 (or it would require #202 due to the nature of TypeScripts type system). Wesley even showed a workaround.
The main reason it was closed was not due to inactivity, but because it was marked a duplicate. |
OK thanks. My bad, I have skipped over that one comment strangely. The work-around should work and I saw it but this issue was more about integrating that logic into TS instead (as I didn't see the duplicates, I took that comment as a refusal to include it in TS). I'm closing that issue now. |
Bug Report
π Search Terms
ArrayBuffer
Uint8Array
π Version & Regression Information
At least in:
β― Playground Link
Playground link with relevant code
π» Code
Also, to be more explicit about the root issue in TypeScript:
π Actual behavior
This is kind of continuation of the since closed due to inactivity #31311 issue, but with other arguments.
The sample codes presented here go through TypeScript's checks despite both leading to a browser TypeError because
DataView
's constructor expects anArrayBuffer
, not anUint8Array
(on FireFox, the error is:TypeError: DataView: expected ArrayBuffer, got Uint8Array
).From the linked issue, it seems that TypeScript considers an Uint8Array to be a valid ArrayBuffer because they are structurally the same. But there are subtle differences like this one.
I understand the point of view that TypeScript is not supposed to be perfectly sound, but I think that it would be nice if TypeScript caught those types of mistakes.
We had an issue related to this and I think that we wouldn't have it with plain JavaScript, because we would have been more careful with types! In the end, not always thinking about these types of mistakes is a major reason for switching to TS.
π Expected behavior
I would expect that an ArrayBuffer and a Uint8Array to not be considered the same thing.
The text was updated successfully, but these errors were encountered: