Skip to content

Overload is not recognized correctly in some cases #49820

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Closed
mochiya98 opened this issue Jul 7, 2022 · 3 comments
Closed

Overload is not recognized correctly in some cases #49820

mochiya98 opened this issue Jul 7, 2022 · 3 comments
Labels
Design Limitation Constraints of the existing architecture prevent this from being fixed Fix Available A PR has been opened for this issue

Comments

@mochiya98
Copy link

mochiya98 commented Jul 7, 2022

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

@RyanCavanaugh RyanCavanaugh added the Design Limitation Constraints of the existing architecture prevent this from being fixed label Jul 7, 2022
@RyanCavanaugh
Copy link
Member

See #30134 - inference only does a fixed number of passes, which isn't an algorithm capable of resolving this.

@PabloLION
Copy link

PabloLION commented Sep 30, 2022

Here's a shorter MRE to address/demonstrate the problem. I hope it can be helpful:

type Fruit = "apple" | "banana";

function printFruit(fruit: "apple");
function printFruit(fruit: "banana");
function printFruit(fruit: Fruit) {
  console.log(fruit);
}

function callPrintFruit(fruit: Fruit) {
  printFruit(fruit); // Error here: No overload matches this call.
}

Playground: https://www.typescriptlang.org/play?ts=4.7.4#code/C4TwDgpgBAYgTgVwJbCgXigIgIZjAGwkygB8sAjbAO2u0wG4AoRgMwSoGNgkB7KqMHCRVg8ZMAAULRCgBcWXASIBKJm07c+AoSLEopM4PMyUaNTKtbsuvfoOGjDB8fL3BlUAN6MoUDnwBnHkIAOnweAHNnFEsAX2Z1Gy0ObHx8AAUdR3Foo1hDD29fe10naXFVKAB6KqgAUTg4HjgoAAsIOAh5ADkeKB4ANw7w7AATKABbbGAOdoCoYFakeZS0kMZ4oA

@Septh
Copy link

Septh commented Dec 19, 2023

I was about to file a similar issue but found this one. To build on @PabloLION's comment, the following variation compiles OK (playground), which seems strange to me because the error is on the function call, not the function's body:

type Fruit = "apple" | "banana";

declare function printFruit(fruit: "apple"): void;
declare function printFruit(fruit: "banana"): void;
declare function printFruit(fruit: Fruit): void;

function callPrintFruit(fruit: Fruit) {
  printFruit(fruit); // No error here!
}

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
Design Limitation Constraints of the existing architecture prevent this from being fixed Fix Available A PR has been opened for this issue
Projects
None yet
Development

Successfully merging a pull request may close this issue.

5 participants