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

Overloding methods in class without declare #2260

Open
rpominov opened this issue Aug 16, 2016 · 11 comments
Open

Overloding methods in class without declare #2260

rpominov opened this issue Aug 16, 2016 · 11 comments

Comments

@rpominov
Copy link

Using declare we can overload methods in classes:

declare class Foo {
  bar(a: number, b: string): number;
  bar(b: string): number;
}

But is it possible without declare?
Here is a not working example just to illustrate what I mean:

class Foo {
  bar(a: number, b: string): number; 
  bar(b: string): number;
  bar(aOrB, maybeB) {
    return maybeB !== undefined ? maybeB : aOrB;
  }
}
@andreypopp
Copy link
Contributor

See intersection types example:

/* @flow */
class Foo {}
class Bar {}
declare var f: ((x: Foo) => void) & ((x: Bar) => void);
f(new Foo());
f(new Bar());
f(true); // Flow will error here.

It should work outside of declarations too.

@rpominov
Copy link
Author

Right. But how do I use this inside class {}?

@rpominov
Copy link
Author

Just to clarify: you've made function f overloaded, but I need to make a class method overloaded.

@andreypopp
Copy link
Contributor

@rpominov ah... right, not sure how it fits with method types declarations. But the case you described can be typed with:

class Foo {

  bar(aOrB: number | string, maybeB?: string): number {
    return maybeB !== undefined ? maybeB : aOrB;
  }
}

right?

@andreypopp
Copy link
Contributor

@rpominov disregard my last response, you have a dependency between args. Waiting for some smarter one to answer that then.

@rpominov
Copy link
Author

Not quite same thing.

In your version bar(1) is allowed, while with proper overloading if second argument not provided first one must be a string.

But anyway, this is not my real case. I need this for a static method similar to Promise.all().

@vkurchatkin
Copy link
Contributor

I don't think it's possible. The easiest way around is to have a separate declaration and cast implementation to declaration type via any.

@rpominov
Copy link
Author

That is an option. But will I have to duplicate all other methods in declaration and in implementation then?

@vkurchatkin
Copy link
Contributor

But will I have to duplicate all other methods in declaration and in implementation then?

Yes, I think that's the only option without affecting runtime behaviour

@rpominov
Copy link
Author

Seems like a room for improvement for Flow then. There should be a way to do this somehow without duplication.

@avikchaudhuri
Copy link
Contributor

The problem is, it's hard to check for Flow that a method body satisfies multiple signatures. Which is why we implemented it for declare class first, since they have no method bodies. :P

This should be fixed.

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

No branches or pull requests

4 participants