Skip to content

Overloaded union type does not resolve overloads #37704

@maximilianMairinger

Description

@maximilianMairinger

Two interfaces with (at the same signatures) overloaded functions as members, combined into an union type, loose the overload information.

TypeScript Version: 3.9.0-dev.20200328

Search Terms:

Union Overload resolve interface

Code

type Wrap<T> = {wrap: T}

type A<Values extends any[]> = {
  get(): Values
  get(overload: string): Wrap<Values>
}

type B<Value> = {
  get(): Value
  get(overload: string): Wrap<Value>
}

type AB<Values extends any[]> = A<Values> | B<Values[number]> 

let ab: AB<(string | number)[]>
let res = ab.get("overload") // typeof (string | number | Wrap<(string | number)[]>)

res should be only typeof (Wrap<string | number> | Wrap<(string | number)[]>) since both subtypes (A and B) of the union type (AB) have the same overload signatures

Expected behavior:

res in the above code should be the same as A and B seperatly executed.

type AB1<Values extends any[]> = A<Values>
let ab1: AB1<(string | number)[]>
let res1 = ab1.get("overload")

type AB2<Values extends any[]> = B<Values[number]>
let ab2: AB2<(string | number)[]>
let res2 = ab2.get("overload")


let whatResShouldBe: (typeof res1) | (typeof res2)

Actual behavior:

res in the first example is typeof (string | number | Wrap<(string | number)[]>) and not as the union suggests (typeof res1) | (typeof res2) or (Wrap<string | number> | Wrap<(string | number)[]>)

Playground Link

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