Incorrect inference for Uint8Array's underlying source
Closed this issue ยท 3 comments
๐ Search Terms
uint8array, arraylike, sharedarraybuffer
๐ Version & Regression Information
- This is the behavior in every version I tried, and I reviewed the FAQ for entries about Uint8Arrays.
โฏ Playground Link
๐ป Code
function example(x: ArrayBufferLike | ArrayLike<number>): Uint8Array<ArrayBufferLike> {
return new Uint8Array(x)
}๐ Actual behavior
The example function can take an ArrayBuffer, SharedArrayBuffer or a number[] to wrap it in a Uint8Array, but due to ArrayLike<number>, the Uint8Array constructor incorrectly assumes the underlying source will be an ArrayBuffer and rejects x being passed in since it can also be a SharedArrayBuffer.
๐ Expected behavior
The expected behaviour is for the Uint8Array constructor to assume the underlying source will be ArrayBufferLike and not ArrayBuffer.
Additional information about the issue
No response
Seems like #14107 is the underlying issue here; TS has two separate overloads for ArrayBufferLike and ArrayLike<number>, and you cannot call both of them at once. I doubt there's much appetite to add even more complicated construct signatures to handle these edge cases. If you need this yourself you can always just use declaration merging in your own code base like
interface Uint8ArrayConstructor {
new <T extends ArrayBufferLike | Iterable<number> | ArrayLike<number> | number>(buffer: T):
Uint8Array<T extends ArrayBufferLike ? T : ArrayBuffer>;
}and then your code will just work.
Playground link to code
๐ค Thank you for your issue! I've done some analysis to help get you started. This response is automatically generated; feel free to ๐ or ๐ this comment according to its usefulness.
Similar Issues
Here are the most similar issues I found
- (70%) microsoft/typescript#17962: UintArray doesn't accept union of types
- (70%) microsoft/typescript#61835: The second parameter type of `apply` should be `ArrayLike<T>` instead of `T[]`
- (69%) microsoft/typescript#61680: Generic TypedArray constructors have only one overload
- (69%) microsoft/typescript#24266: ArrayBufferView constructors do not accept ArrayBuffer as correct type
- (69%) microsoft/typescript#42534: Uint8Array assignable to ArrayBuffer despite having subtle differences leading to browser TypeErrors
- (69%) microsoft/typescript#52815: Constructor for DataView is too broad
- (68%) microsoft/typescript#61480: AllowSharedBufferSource definition is incorrect
- (68%) microsoft/typescript#50714: ArrayBuffer is not eliminated from union as expected in narrowing of ArrayBuffer | Uint8Array
- (67%) microsoft/typescript#58796: `Uint8Array.prototype.constructor` is missing/incorrect type, and probably much more
- (67%) microsoft/typescript#61793: Cannot assign Uint8Array type to Uint8Array variable created with constructor
- (67%) microsoft/typescript#57972: ArrayBuffer.isView not narrowing type
- (67%) microsoft/typescript#6221: All typed arrays equal
- (66%) microsoft/typescript#54258: `DataView.buffer` should be `ArrayBufferLike` instead of `ArrayBuffer`
- (66%) microsoft/typescript#60846: Incorrect type returned by TextEncoder.encode
- (66%) microsoft/typescript#62240: Revert ts5.9 changes for Uint8Array
If your issue is a duplicate of one of these, feel free to close this issue. Otherwise, no action is needed.
This issue has been marked as "Design Limitation" and has seen no recent activity. It has been automatically closed for house-keeping purposes.