Skip to content

Float16Array's definition is incompatible with other typedarraysΒ #62343

@greggman

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

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