-
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
TS allows adding Uint8Array item to Array<ArrayBuffer> #31311
Comments
Sure, |
@nmain But a Uint8Array is not an ArrayBuffer, two very different objects. You can't mix them. |
Read up on structural typing https://www.typescriptlang.org/docs/handbook/type-compatibility.html, and then wrap your head around this example: In short, all it matters is that |
I don't need to, I know what it is. Read my comment #31311 (comment) |
I'm sorry my example wasn't clear enough for you. Let me show you the exact definition for interface ArrayBuffer {
readonly byteLength: number;
slice(begin: number, end?: number): ArrayBuffer;
} (Leaving off Any object that has these methods and properties is compatible with an ArrayBuffer. It doesn't matter if there's some behavioral difference between them, because TypeScript does not measure those things and TypeScript does not use nominal typing. In the example I posted, I give an object which is obviously not an ArrayBuffer -
|
??? Infinite loop detected. I already answered. You keep explkainig what TS does. I already know what TS does. And it's wrong, because ArrayBuffer != Uint8Array. I already know TS thinks otherwise — if it was different I would not have to open this issue. I *cannot use an array of mixed ArrayBuffer and Uint8Array, e.g. to concatenate them — because they are not the same and it would not work (during runtime). |
FYI, in your own project, you can make the various interface ArrayBuffer {
" buffer_kind"?: "array";
}
interface Uint8Array {
" buffer_kind"?: "uint8";
} we briefly trialed using |
This issue has been marked as a 'Duplicate' and has seen no recent activity. It has been automatically closed for house-keeping purposes. |
Search Terms: ArrayBuffer, Uint8Array
Code
Playground link
Expected behavior:
Both test cases should result in type errors.
Actual behavior:
Only case 2 results in an error, case 1 does not.
The text was updated successfully, but these errors were encountered: