-
Notifications
You must be signed in to change notification settings - Fork 12.8k
Union type conflicts with overloaded function #16816
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
Comments
I've wondered about unions/intersections of parameters to overloaded/generic functions before. My guess is that it's a good thing to want but might be too expensive for the type checker to do this. Can anyone link to a canonical issue for this type of thing? Of course in your case you can work around this in any number of ways. The best way is probably to declare the function to take If you are trying to fix without redeclaring the function, you can cast it: function foos2(f: Foo | Foo[]) {
(foos as (f: Foo | Foo[]) => any)(f); // success
} And in general, you can force TypeScript to notice that an overloaded or generic function can take a union: var intersectFunction:
<A1, R1, A2, R2> (func: ((a: A1) => R1) & ((a: A2) => R2)) => ((a: A1 | A2) => R1 | R2)
= (func => func);
function foos2(f: Foo | Foo[]) {
intersectFunction<Foo,any,Foo[],any>(foos)(f); // success
}; I agree it would be nice if TypeScript just noticed without being forced to. It doesn't, because _________________________ (someone fill in the blank pls, thx!) |
If I search for "generic", "overload", "union", and "intersection", issue #14107 is in the top several results, so it was probably just myopia on my part. Thanks! |
Automatically closing this issue for housekeeping purposes. The issue labels indicate that it is unactionable at the moment or has already been addressed. |
TypeScript Version: 2.4.1
Code
Expected behavior:
foos
acceptsFoo
andFoo[]
.foos2
acceptsFoo | Foo[]
. I thinkfoos2
can just pass its argument tofoos
without problems.Actual behavior:
The text was updated successfully, but these errors were encountered: