Skip to content

Overload is not recognized correctly in some casesΒ #49820

Closed
@mochiya98

Description

@mochiya98

Bug Report

πŸ”Ž Search Terms

generics overload

πŸ•— Version & Regression Information

  • 4.7.4(current), 4.8.0-beta(beta)

⏯ Playground Link

Playground link with relevant code

πŸ’» Code

type A1<T> = { type: "a1", v: T };
type B1<T> = { type: "b1", v: T };
type A2 = { a2: string };
type B2 = { b2: string };

function fn<T>(p1: (pp1: 0) => A1<T>, p2: (pp2: A2) => 0): void;
function fn<T>(p1: (pp1: 0) => B1<T>, p2: (pp2: B2) => 0): void;
function fn<T>(
  p1:
    | ((pp1: 0) => A1<T>)
    | ((pp1: 0) => B1<T>),
  p2:
    | ((pp2: A2) => 0)
    | ((pp2: B2) => 0)
) {}

const valA1: A1<string> = ({ type: "a1", v: "" });
const valB1: B1<string> = ({ type: "b1", v: "" });

// expect A
fn((ap1) => valA1, (ap2) => 0);
fn((ap1) => valA1, (ap2: A2) => 0); 
fn((ap1) => valA1, (ap2: any) => 0);
fn((ap1) => valA1, (ap2: unknown) => 0);
fn((ap1: 0) => valA1, (ap2) => 0);
// expect B
fn((bp1) => valB1, (bp2) => 0); // but it will be A, only this will result in an error
fn((bp1) => valB1, (bp2: B2) => 0); 
fn((bp1) => valB1, (bp2: any) => 0);
fn((bp1) => valB1, (bp2: unknown) => 0);
fn((bp1: 0) => valB1, (bp2) => 0);

πŸ™ Actual behavior

No overload matches this call.
  Overload 1 of 2, '(p1: (pp1: 0) => A1<string>, p2: (pp2: A2) => 0): void', gave the following error.
    Type 'B1<string>' is not assignable to type 'A1<string>'.
      Types of property 'type' are incompatible.
        Type '"b1"' is not assignable to type '"a1"'.
  Overload 2 of 2, '(p1: (pp1: 0) => B1<string>, p2: (pp2: B2) => 0): void', gave the following error.
    Argument of type '(bp2: A2) => 0' is not assignable to parameter of type '(pp2: B2) => 0'.
      Types of parameters 'bp2' and 'pp2' are incompatible.
        Property 'a2' is missing in type 'B2' but required in type 'A2'.(2769)

πŸ™‚ Expected behavior

No errors

Metadata

Metadata

Assignees

No one assigned

    Labels

    Design LimitationConstraints of the existing architecture prevent this from being fixedFix AvailableA PR has been opened for this issue

    Type

    No type

    Projects

    No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions