镜像站点 · 本页由第三方 GitHub 只读镜像提供,非 GitHub 官方站点,不接受任何登录或凭据输入。前往 github.com
Skip to content

Float16Array's definition is incompatible with other typedarrays #62343

Description

@greggman

🔎 Search Terms

Float16Array

🕗 Version & Regression Information

  • Tried 5.9.2
  • I was unable to test this on prior versions because irrelevant

⏯ Playground Link

https://www.typescriptlang.org/play/?target=99#code/KYDwDg9gTgLgBDAnmYcAqzgBMCCUoCGiAwhAHYDOMUArgMYzRwC8AUHB3AD5wCSZMABx5CJclVoNo7TjwCqASwHD8RUpWr1GUGR3lKhxADYEAtilyqxGydt3c+AgIwA2EWvGapOzg8XO3K3UJLWlfHn4YAGYAJndrEO97fQFY+OCvO3C4ADEjCAIYV3TPWyYAenK4OghTU2ABBAALBQo4IyVUCBoYABpm1GB8JhqsYDaCAHciOAomiEmlAHNmwtz8wuKg0tCoOFa4Mgh4GvNChQAjI1RFmCa4Y6ah6p3vCmT1gui47Ztdj7yXxcABYSn9vABuVisUCQWAITDoTCWUQsD6RFSiD7+ITxbEGQTGMwWPHZSJbLHZHEUojo1I-SmyOA4tJWAEbIqBRl6T6FVnchyAwog+JQmHgaAnEwUNoYElWABqCmAkwA4g0hoUmABvewEKwAIRoADNjUMAFxweJG01DKG+C6IGDAADypoowBglrINFMFzt0N8NXB2gAFBQFAAvYD8A1O8be33+qAASjgut8vjurQAdPrRDazXtmIcVVbDSai+GozGyHHnRQU-bMxxsxQc47nW7jR74CWAAzNjgAX3sS09SpVAB40HBQM6yFhZcj4gA+UPEbSWuXYMGJbT9H2mACi13qAgoib9QxT2-T9iDnjgADdlZMWKX35voKG23mK7aUD9H+nauu6nqHr6p7AOeMCNkOmYgfG3a9nAADUJaviqHbxgAMg0Sx3AhvhQJ6NBQGQL5vnABCygho6juKcLwMaNBkAwCjkHA44wJOkwznOIALkuSLyqI675kQhYWuWBaVkM-TflA24rr8+7QLe6D3o+GhUWWJZkGWSmhpJiDSamCGkTA5GUVh760egUKjkAA

💻 Code

export type TypedArrayConstructor =
    | Int8ArrayConstructor
    | Uint8ArrayConstructor
    | Uint8ClampedArrayConstructor
    | Int16ArrayConstructor
    | Uint16ArrayConstructor
    | Int32ArrayConstructor
    | Uint32ArrayConstructor
    | Float16ArrayConstructor // comment this line out, the error codes away showing that Float16ArrayConstructor is not compatible with other constructors
    | Float32ArrayConstructor
    | Float64ArrayConstructor;

export type TypedArray =
    | Int8Array
    | Uint8Array
    | Uint8ClampedArray
    | Int16Array
    | Uint16Array
    | Int32Array
    | Uint32Array
    | Float16Array
    | Float32Array
    | Float64Array;

export class TypedArrayViewGenerator {
    arrayBuffer: ArrayBuffer;
    byteOffset: number;

    constructor(sizeInBytes: number) {
        this.arrayBuffer = new ArrayBuffer(sizeInBytes);
        this.byteOffset = 0;
    }
    getView<T extends TypedArray>(Ctor: TypedArrayConstructor, numElements: number): T {
        // gets an error here because `Float16Array` is not following the
        // the same pattern as the other typed array views
        const view = new Ctor(this.arrayBuffer, this.byteOffset, numElements);
        this.byteOffset += view.byteLength;
        return view as T;
    }
}

export function getView<T extends TypedArray>(arrayBuffer: ArrayBuffer, Ctor: TypedArrayConstructor): T {
    // this gets an error too for the same reason
    const view = new Ctor(arrayBuffer);
    return view as T;
}

🙁 Actual behavior

Float16Array should behave the same as other typed array views like Float32Array, Uint32Array etc... It's not

🙂 Expected behavior

Float16Array behaves the same as other typed array views like Float32Array, Uint32Array etc...

Additional information about the issue

No response

Activity

  1. greggman commented on Aug 27, 2025

    @greggman
    ContributorAuthor

    I think maybe one or both of these lines

        new (buffer: ArrayBuffer, byteOffset?: number, length?: number): Float16Array<ArrayBuffer>;
        new (array: ArrayLike<number> | ArrayBuffer): Float16Array<ArrayBuffer>;

    need to be added to esnext.float16.d.ts here?

    I see similar definitions for other typed arrays. For example

    new (buffer: ArrayBuffer, byteOffset?: number, length?: number): Float64Array<ArrayBuffer>;

  2. Renegade334 commented on Aug 28, 2025

    @Renegade334
    Contributor

    Looks like the float16 definitions were written prior to #60934 but merged after it, and you're running afoul of the same problem as in the linked issue there. Adding the missing non-generic overloads should indeed solve the problem.

  3. locked as resolved and limited conversation to collaborators on Mar 9, 2026
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

    BugA bug in TypeScriptDomain: lib.d.tsThe issue relates to the different libraries shipped with TypeScriptHelp WantedYou can do this

    Type

    No type

    Projects

    No projects

      Relationships

      None yet

      Development

      No branches or pull requests

      Issue actions