Skip to content

Incorrect constructor overload: takes only first overload, but not others #60745

@psnet

Description

@psnet

🔎 Search Terms

constructor, typescript, Uint32Array, Uint16Array, Uint8Array, Uint32ArrayConstructor, Uint16ArrayConstructor, Uint8ArrayConstructor

🕗 Version & Regression Information

  • This changed between versions 5.6.3 and 5.7.2

⏯ Playground Link

https://www.typescriptlang.org/play/?target=9&ts=5.7.2#code/FAFwngDgpgBAkgOwCZQB4FEA2UC2UEgDOAwgPYKEgBOArgMYilUwC8MAqgJYEDMATAEEqVAIZgyFavUbMAPh24gAjADYho8eUq0GTGPK4EAHOrETt0pgG5gwAGY0EDTuRggolABR1SOCOXwQABVIKAAuGAQaHAAjKCoAGhg6AAtHAGsAEREQEQAhbgjTMDyaOzt4gEoYAG9gGAaYbBAYADdOKAB3EOgIxBQMbDwCEi0pXSobesbCTs4QVJhvX38EQJ6oarrGneSRQlgAdSgYgHEAGT4AJXwUKm4Acwl3VBAAOnYAOQBlOFPP9CZAD6cE+QTC012u3aXQ2rAUvEEwjEUyhUJiVCgInSqLRdH2RxOF2ut3ij2eaHeX1+-0BQO+AAkAPJXcGQtENGHdULwwzKNTIsC4jkNDFYnG2Dn4g4wY5nS43ZBkhBPcgvKk-P4A4F5ACaQXQEJFjS5cLYfJMguFIrF2OtOxQdhENEwICNxrcKSopE6kS6MHQwiYngABgAiHx+AIEDZhmCcQiRUgtalaumgoJJVO04GMllBGB6bPaoF6g1LAAkNUjq3WoQAvpUQ5UbDt65LGj5JHtMHQXTkoEhSuV4kzygcWmwACytztjHt9zADodlCpUc74B4gFLwozWzEgGhUBB+32m0LeNIILI5fLcJL43v99wrkdUMd2CcPkRPpcv4drhuKrbi2wDtkAA

💻 Code

type IndexElementsConstructor = Uint32ArrayConstructor | Uint16ArrayConstructor | Uint8ArrayConstructor;

function test(componentType: number, chunkDataBin: ArrayBuffer) {
    let viewType: IndexElementsConstructor;

    switch (componentType) {
        case WebGL2RenderingContext.UNSIGNED_INT:
            viewType = Uint32Array;

            break;

        case WebGL2RenderingContext.UNSIGNED_SHORT:
            viewType = Uint16Array;

            break;

        case WebGL2RenderingContext.UNSIGNED_BYTE:
            viewType = Uint8Array;

            break;

        default:
            throw new Error(`"componentType" is not UNSIGNED_INT, UNSIGNED_SHORT or UNSIGNED_BYTE (${componentType})`);
    }

    const calculatedBufferOffset = 4;
    const calculatedBufferLength = 8;

    return new viewType(chunkDataBin, calculatedBufferOffset, calculatedBufferLength);
}

🙁 Actual behavior

Image

🙂 Expected behavior

Image

Additional information about the issue

If change code to simpler variant:

type IndexElementsConstructor = Uint32ArrayConstructor | Uint16ArrayConstructor | Uint8ArrayConstructor;

function test(componentType: number, chunkDataBin: ArrayBuffer) {
    const viewType: IndexElementsConstructor = Uint32Array;

    const calculatedBufferOffset = 4;
    const calculatedBufferLength = 8;

    return new viewType(chunkDataBin, calculatedBufferOffset, calculatedBufferLength);
}

newest TS version recognizes this.

Metadata

Metadata

Assignees

No one assigned

    Labels

    Design LimitationConstraints of the existing architecture prevent this from being fixed

    Type

    No type

    Projects

    No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions