-
Notifications
You must be signed in to change notification settings - Fork 1.9k
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
function overloading #60
Comments
You can try intersection types. See an example at: http://flowtype.org/docs/union-intersection-types.html#_ |
It's also worth noting that you might need to use parentheses to express the union or intersection of two function types.
is parsed as
so the union of two functions is
|
Thanks. |
No doubt. Currently we do support overloading syntax, at least in some contexts. For example, you can write: declare class C { |
The following should now work, closing.
|
Is it possible to set overloading types for function without using |
Support rest parameter type annotations
@avikchaudhuri Is there a way to do that while also defining the function? |
It does not work in other contexts as @leoasis pointed out. such as when defining a function. Flow is not able to select the right combo.
Throws the following error:
|
Is partially working. From https://stackoverflow.com/questions/45481046/how-to-define-a-type-of-return-based-on-an-argument-string-flowtype-javascript |
Any progress here? I think it's a quite important feature. |
Interfaces seem to have the behaviour you're looking for: #3021 (comment). |
@zeorin Your linked example does not show how one would define a function. I'm not quite sure what it does, actually, there doesn't seem to be any Javascript, just Flow type stuff. How would one use that to do something real, with JS code? You define the function XYZ, how do you annotate it to have multiple type signatures? If you follow my linked issue, I link to examples hat use an intersection type of function signatures, but that doesn't seem to work when I introduce generics, or I don't know how to do that. |
Can someone please provide a fully working example (not just type-only examples)? |
@StreetStrider in the previous example, if I make the implementation parameters type How do we fix that one? |
The error message remains quite good as well. |
That's interesting, I have not seen that method explained anywhere, including Flow docs on Overloaded functions (the example above had only types, no implementation). declare function add (x: number, y: number): number;
declare function add (x: string, y: string): string;
function add (x: any, y: any) { // <----- IMPLEMENTATION
if (typeof x === 'number' && typeof y === 'number')
return x + y;
else if (typeof x === 'string' && typeof y === 'string')
return x + y;
throw '';
}
add(1, 2)
add('asdf', 'asdf')
// type error (good)
add(1, 'asdf')
// type error (good)
add(true, false) But what's the difference? Is there a bug with the intersection method? |
I can't remember how I figured it out, but this is quite a reliable way to do this in both TS and Flow. It kind of creates proper overload, that's all I know. When you use intersection, sometimes it creates an «incorrect» intersection, considering function overload. Maybe someone with internals' knowledge can clarify.
In my opinion, Flow docs was never good. There were a lot of vague spots. So a lot of info became a sort of folklore. It is good to ask questions in tracker from time to time. Sometimes someone with better knowledge may help. This is what helped me in the past. |
I would like to express a function with the following overloading:
it seems to me that it is not possible since :
Does not conditionally constraint the return type, any idea ?
The text was updated successfully, but these errors were encountered: