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

TypedArray-related errors with TypeScript 5.7.2 #58

Open
mourner opened this issue Jan 8, 2025 · 6 comments
Open

TypedArray-related errors with TypeScript 5.7.2 #58

mourner opened this issue Jan 8, 2025 · 6 comments
Labels
help wanted Extra attention is needed

Comments

@mourner
Copy link
Owner

mourner commented Jan 8, 2025

Upgrading TypeScript and then running npm test produces the following errors:

index.js:88:47 - error TS2554: Expected 0-1 arguments, but got 3.

88             this._boxes = new ArrayType(data, byteOffset + 8, numNodes * 4);
                                                 ~~~~~~~~~~~~~~~~~~~~~~~~~~~~

index.js:89:59 - error TS2554: Expected 0-1 arguments, but got 3.

89             this._indices = new this.IndexArrayType(data, byteOffset + 8 + nodesByteSize, numNodes);
                                                             ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~

index.js:99:52 - error TS2554: Expected 0-1 arguments, but got 3.

99             this._boxes = new ArrayType(this.data, 8, numNodes * 4);
                                                      ~~~~~~~~~~~~~~~

index.js:100:64 - error TS2554: Expected 0-1 arguments, but got 3.

100             this._indices = new this.IndexArrayType(this.data, 8 + nodesByteSize, numNodes);

I don't know why it fails to pickup the 3-argument version of the typed constructors. Probably related to microsoft/TypeScript#59417? Any help from people more well versed in TypeScript appreciated.

@mourner mourner added the help wanted Extra attention is needed label Jan 8, 2025
@mourner
Copy link
Owner Author

mourner commented Jan 8, 2025

@rbuckton perhaps you could give a hint on what might be wrong here? The typings depend on a union of *Constructor typed array types here, and for some reason it doesn't resolve to the 3-argument version of the constructor after updating from TS 5.6.

@rbuckton
Copy link

rbuckton commented Jan 8, 2025

When you union constructor types, generic signatures must match exactly. Since each constructor type in the union produces a different return type, they are excluded from the resulting set. In the long term, I can look into adding non-generic overloads for these cases. As a workaround, you can define an alternative union for generic use:

type TypedArray<TArrayBuffer extends ArrayBufferLike = ArrayBuffer> = 
    | Uint8Array<TArrayBuffer>
    | Int8Array<TArrayBuffer>
    | Uint16Array<TArrayBuffer>
    | Int16Array<TArrayBuffer>
    | Uint32Array<TArrayBuffer>
    | Int32Array<TArrayBuffer>
    | Float32Array<TArrayBuffer>
    | Float64Array<TArrayBuffer>;

type TypedArrayConstructor = (
    | Uint8ArrayConstructor
    | Int8ArrayConstructor
    | Uint16ArrayConstructor
    | Int16ArrayConstructor
    | Uint32ArrayConstructor
    | Int32ArrayConstructor
    | Float32ArrayConstructor
    | Float64ArrayConstructor
) & {
    new <TArrayBuffer extends ArrayBufferLike = ArrayBuffer>(arrayBuffer: TArrayBuffer, byteOffset?: number, length?: number): TypedArray<TArrayBuffer>;
};

@mourner
Copy link
Owner Author

mourner commented Jan 8, 2025

@rbuckton thank you for the prompt response! I can't get it to work so far, getting some errors that are cryptic to me, but perhaps I should just silence the error because it is getting into territory too complicated for simple typing I intended for the library.

My JSDoc looks like this:

/** @template {ArrayBufferLike} T
 * @typedef {Uint8Array<T> | Int8Array<T> | Uint16Array<T> | Int16Array<T> | Uint32Array<T> | Int32Array<T> | Float32Array<T> | Float64Array<T>} TypedArray<T = ArrayBuffer> */

/** @typedef {(Int8ArrayConstructor | Uint8ArrayConstructor | Uint8ClampedArrayConstructor | Int16ArrayConstructor | Uint16ArrayConstructor | Int32ArrayConstructor | Uint32ArrayConstructor | Float32ArrayConstructor | Float64ArrayConstructor) & {new <T extends ArrayBufferLike = ArrayBuffer>(arrayBuffer: T, byteOffset?: number, length?: number): TypedArray<T>}} TypedArrayConstructor */

And one of the errors is:

index.js:44:49 - error TS2345: Argument of type 'Int8ArrayConstructor | Uint8ArrayConstructor | Uint8ClampedArrayConstructor | Int16ArrayConstructor | ... 4 more ... | Float64ArrayConstructor' is not assignable to parameter of type 'TypedArrayConstructor | undefined'.
  Type 'Uint8ClampedArrayConstructor' is not assignable to type 'TypedArrayConstructor | undefined'.
    Type 'Uint8ClampedArrayConstructor' is not assignable to type 'Int8ArrayConstructor & (new <T extends ArrayBufferLike = ArrayBuffer>(arrayBuffer: T, byteOffset?: number | undefined, length?: number | undefined) => TypedArray<...>)'.
      Type 'Uint8ClampedArrayConstructor' is not assignable to type 'Int8ArrayConstructor'.
        The types of 'prototype.filter(...)[Symbol.toStringTag]' are incompatible between these types.
          Type '"Uint8ClampedArray"' is not assignable to type '"Int8Array"'.

44         return new Flatbush(numItems, nodeSize, ArrayType, undefined, data, byteOffset);
                                                   ~~~~~~~~~

@rbuckton
Copy link

rbuckton commented Jan 9, 2025

This is tracked by microsoft/TypeScript#60745.

@rbuckton
Copy link

rbuckton commented Jan 9, 2025

Do the union constituents match what they were previously? From the error you provided it sounds like Uint8ClampedArray might not have been included previously?

@mourner
Copy link
Owner Author

mourner commented Jan 10, 2025

@rbuckton good catch, that was one of the problems. I managed to get the typecheck passing in #59, but it looks ugly as hell now — not sure whether I should merge this or wait for new TS versions if they're going to address cases like this...

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
help wanted Extra attention is needed
Projects
None yet
Development

No branches or pull requests

2 participants