Skip to content
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

ReturnType<SomeClass["someMethodWithOverloads"]> incorrect result #42510

Closed
david-fong opened this issue Jan 27, 2021 · 4 comments
Closed

ReturnType<SomeClass["someMethodWithOverloads"]> incorrect result #42510

david-fong opened this issue Jan 27, 2021 · 4 comments

Comments

@david-fong
Copy link

Bug Report

🔎 Search Terms

  • ReturnType
  • overload

🕗 Version & Regression Information

  • This is the behavior in every version I tried (on the playground, the lowest version is 3.3.3), and I reviewed the FAQ for entries about function and method typings.

⏯ Playground Link

Playground link with relevant code

💻 Code

class Class {
    public method(param: 0): boolean;
    public method(param: 1): string;
    public method(param: 2): this;
    public method(param: number): boolean | string | this {
        switch (param) {
            case 0: return true;
            case 1: return "string";
            case 2: return this;
            default: throw new Error();
        }
    }
}
type TypeAlias = ReturnType<Class["method"]>; // is `Class`. expected `boolean | string | Class`;

🙁 Actual behavior

If you shuffle the order of the overload definitions, the last one (the non-implementation one) becomes the type given by ReturnType<Class["method"]>.

🙂 Expected behavior

The return type indicated by the method signature that provides the function body should be given by ReturnType<Class["method"]>

@MartinJohns
Copy link
Contributor

Conditional types do not work with overloads. This is mentioned in the documentation.

@david-fong
Copy link
Author

david-fong commented Jan 27, 2021

Conditional types do not work with overloads. This is mentioned in the documentation.

I see. I wish the Conditional Types page on the typescript website made this clearer. Thank you, Martin for explaining and sorry I didn't catch this before I made this issue! Feel free to close it.

@david-fong
Copy link
Author

For anyone that stumbles upon this issue I mistakenly made, here are a couple of related links:

I clearly was not looking hard enough. Again, sorry.

@MartinJohns
Copy link
Contributor

MartinJohns commented Jan 27, 2021

I wish the Conditional Types page on the typescript website made this clearer.

Type inference in conditional types

When inferring from a type with multiple call signatures (such as the type of an overloaded function), inferences are made from the last signature (which, presumably, is the most permissive catch-all case). It is not possible to perform overload resolution based on a list of argument types.

But I've noticed that the v2 handbook (which is marked as work in progress) does not mention this constraint at all.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

No branches or pull requests

2 participants