-
Notifications
You must be signed in to change notification settings - Fork 12.8k
Generic TypedArray constructors have only one overload #61680
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
Comments
I don't understand what you're trying to do here. In the case where you're passing a |
It's a derived class, similar to this, which I've still trimmed for brevity (playground): type StringEncoding = "cp437" | "utf8";
export class EncodedString extends Uint8Array {
static getEncoder(encoding: StringEncoding): TextEncoder {
throw new Error("not implemented");
}
constructor(encoding: StringEncoding, value: string)
constructor(encoding: StringEncoding, value: ArrayBufferLike, byteOffset?: number, byteLength?: number)
constructor(encoding: StringEncoding, value: string | ArrayBufferLike, byteOffset?: number, byteLength?: number) {
if (typeof value === 'string') {
const encoded = EncodedString.getEncoder(encoding).encode(value)
super(encoded);
// ^^^^^^^ this is a compiler error if the base class is `Uint8Array<ArrayBufferLike>`
} else {
super(value, byteOffset, byteLength);
// ^^^^^ this is a compiler error if the base class is `Uint8Array`
}
}
} If I give no type parameters for Perhaps it is a little odd what I'm trying to do, i.e. subclassing |
π Search Terms
Uint8Array
π Version & Regression Information
Uint8Array
is not generic in earlier versionsβ― Playground Link
https://www.typescriptlang.org/play/?target=11&ts=5.8.3#code/MYewdgzgLgBBIFcBOwCmMC8MyoO4wFUBLMKADgEEkkBDATwAoBGABgEoBuAKC9EllAAHOk0zY8hEuSq1G8ZGk69w0GELoAmMTnzFSlavQA8M+gCEEAM0uokAGSIBrVAD4G8lKiV9V6gMzaEnrShnQmoRbWtm4eitxAA
π» Code
π Actual behavior
Only the generic constructor overload of
Uint8Array
is available when usingUint8Array
with a generic parameter, i.e. the one which accepts aTArrayBuffer
as the first argument. It is therefore not possible to pass aUint8Array
as the first argument without a compile error, even though this is valid.π Expected behavior
It should be possible to use all the valid overloads of
Uint8Array
(and friends) constructor without a compiler error.Additional information about the issue
This seems to only affect
target
value ofES2024
orESNext
, but I can't figure out why from looking at the types.I assume this behaviour occurs for the same reason as given in this comment. In that case I assume the fix would be adding copies of all the constructor overloads to the
Uint8ArrayConstructor
interface, but with type parameters.For example, the following works (TS Playground):
I think this is basically the opposite problem to that which was fixed by #60934.
The text was updated successfully, but these errors were encountered: