Skip to content
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

Closed
peaBerberian opened this issue Jan 28, 2021 · 2 comments

Comments

@peaBerberian
Copy link

peaBerberian commented Jan 28, 2021

Bug Report

πŸ”Ž Search Terms

ArrayBuffer
Uint8Array

πŸ•— Version & Regression Information

At least in:

  • typescript 4.1.3
  • nightly

⏯ Playground Link

Playground link with relevant code

πŸ’» Code

var a = new Uint8Array([1, 2, 3]);
new DataView(a);

Also, to be more explicit about the root issue in TypeScript:

function createDataView(buffer : ArrayBuffer) : DataView {
  return new DataView(buffer);
}

var a = new Uint8Array([1, 2, 3]);
createDataView(a);

πŸ™ 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 an ArrayBuffer, not an Uint8Array (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.

peaBerberian added a commit to canalplus/rx-player that referenced this issue Jan 28, 2021
…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).
peaBerberian added a commit to canalplus/rx-player that referenced this issue Jan 28, 2021
…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).
peaBerberian added a commit to canalplus/rx-player that referenced this issue Jan 28, 2021
…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).
@MartinJohns
Copy link
Contributor

MartinJohns commented Jan 28, 2021

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.

since closed due to inactivity

The main reason it was closed was not due to inactivity, but because it was marked a duplicate.

@peaBerberian
Copy link
Author

OK thanks.

My bad, I have skipped over that one comment strangely.
I saw the last comment in the issue write about a "duplicate" but I didn't see which one it was speaking of.

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.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

No branches or pull requests

2 participants