-
Notifications
You must be signed in to change notification settings - Fork 12.6k
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
Unexpected union inference because of generic function argument #16107
Comments
I'm not sure I see why inferring a union type is incorrect. After all, there are no declared constraints on |
Because by having a type argument, the expectation for it is to be resolved to a single type. I think I've read it somewhere in the docs that type inference doesn't create types and it doesn't infer unions unless they are provided by the programmer. If that were not the case, how would a type variable be constrained to a single type? It seems counter intuitive to me for a function such as PS: The current behaviour creates some discrepancies. E.g. a variation of the example from #138: declare var f: <R>(s: string, n: number) => R;
declare var g: <T>(x: T, y: T) => T;
f = g; // good, error with #16368
declare function test1(h: typeof f): void;
declare function test2<R>(h: (s: string, n: number) => R): void;
test1(g); // good, error with #16368
test2(g); // no error? how is this different from test1 Edit: I've been wrong, declare function test1(h: <R>(s: string, n: number) => R): void; Here, the type parameter In |
I'm not sure I can give a more convincing example. Type variables are inferred to a single type in all locations except for function arguments. And even there it's not always the case as the code from my previous comment shows. I believe that generic inference behaviour should be uniform across all uses. declare function test3<R>(x: R, y: R): void;
declare function test4<R>(h: (s: string, n: number) => R, x: R, y: R): void;
test3(3, 'str'); // good, error with #16368
test4(g, 3, 'str'); // no error :(, what's changed? |
TypeScript Version: nightly (2.4.0-dev.20170502)
TypeScript improperly infers a union type when a generic function is used as an argument. Such an inference is unexpected unless the union constituent is present, or the type arguments are manually provided by the programmer.
Code
The text was updated successfully, but these errors were encountered: