Skip to content

Union between Uint8Array and Array<number> does not work for reduce function #44593

@fizker

Description

@fizker

Bug Report

The compiler cannot find an intersection between the reduce function for number[] and Uint8Array.

From the error message, it looks like the compiler is raising an issue because of the fourth parameter having different types, but since I don't use the fourth parameter, it hardly seems relevant in this specific case.

🔎 Search Terms

  • Uint8Array

🕗 Version & Regression Information

I see this issue in v3.3.3333, v4.3.2 (current stable) and v4.4.0-dev.20210615 (current nightly).

⏯ Playground Link

Playground link with relevant code

💻 Code

// This function fails
function combined(x: Uint8Array | Array<number>): number {
  return x.reduce((s: number, x: number) => s + x * x, 0)
}

// This function has no issues
function uiint8array(x: Uint8Array): number {
  return x.reduce((s: number, x: number) => s + x * x, 0)
}

// This function has no issues
function numberarray(x: Array<number>): number {
  return x.reduce((s: number, x: number) => s + x * x, 0)
}

🙁 Actual behavior

The code works fine at runtime, but the typescript compiler raises the following error for the Uint8Array | Array<number> case:

This expression is not callable.
  Each member of the union type '{ (callbackfn: (previousValue: number, currentValue: number, currentIndex: number, array: Uint8Array) => number): number; (callbackfn: (previousValue: number, currentValue: number, currentIndex: number, array: Uint8Array) => number, initialValue: number): number; <U>(callbackfn: (previousValue: U, currentValue: numb...' has signatures, but none of those signatures are compatible with each other.(2349)
(property) reduce: {
    (callbackfn: (previousValue: number, currentValue: number, currentIndex: number, array: Uint8Array) => number): number;
    (callbackfn: (previousValue: number, currentValue: number, currentIndex: number, array: Uint8Array) => number, initialValue: number): number;
    <U>(callbackfn: (previousValue: U, currentValue: number, currentIndex: number, array: Uint8Array) => U, initialValue: U): U;
} | {
    ...;
}

🙂 Expected behavior

No errors are produced.

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