Skip to content

TypedArray-related errors with TypeScript 5.7.2 #58

Closed

Description

@mourner

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.

Activity

  1. added
    help wanted
    on Jan 8, 2025
  2. mourner commented on Jan 8, 2025

    @mourner
    OwnerAuthor

    @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.

  3. rbuckton commented on Jan 8, 2025

    @rbuckton

    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>;
    };
  4. mourner commented on Jan 8, 2025

    @mourner
    OwnerAuthor

    @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);
                                                       ~~~~~~~~~
  5. rbuckton commented on Jan 9, 2025

    @rbuckton

    This is tracked by microsoft/TypeScript#60745.

  6. rbuckton commented on Jan 9, 2025

    @rbuckton

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

  7. mourner commented on Jan 10, 2025

    @mourner
    OwnerAuthor

    @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...

  8. mourner commented on Mar 10, 2025

    @mourner
    OwnerAuthor

    Nice to see all these issues resolved after upgrading to a newer TS version!

  9. added a commit that references this issue on Jul 6, 2025
    3a6f0f0
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Metadata

Metadata

Assignees

No one assigned

    Labels

    help wanted

    Type

    No type

    Projects

    No projects

      Milestone

      No milestone

      Relationships

      None yet

      Development

      No branches or pull requests

      Issue actions

      ⌂ Proxy