No differentiation between ArrayBuffer
and Uint8Array
#55032
Labels
Duplicate
An existing issue was already created
ArrayBuffer
and Uint8Array
#55032
Bug Report
TypeScript can not differentiate between
ArrayBuffer
andUint8Array
(or other typed array types).🔎 Search Terms
Related #50714, #42534, #202
🙁 Actual behavior
TypeScript checks the program above, and reports no errors.
🙂 Expected behavior
An error should be thrown, because in many APIs, passing a
Uint8Array
rather than anArrayBuffer
will throw or result in runtime errors.Cause and proposed fix
The cause for this issue is TypeScript's structural typing. Within the type structure,
ArrayBuffer
is a subset of structure ofUint8Array
. Because of this, TypeScript considersUint8Array
a validArrayBuffer
type.To fix this, we should consider adding a brand to the
Uint8Array
andArrayBuffer
interfaces, such as the following:This may be an issue when subclassing
ArrayBuffer
, because the subclass may have a differentSymbol.toStringTag
The text was updated successfully, but these errors were encountered: