-
Notifications
You must be signed in to change notification settings - Fork 12.8k
Declaration order dependence when inferring to a union #45603
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
The thing that makes this particularly bad is that which call errors is dependent on whether |
That looks great! However I simplified the problem a bit to a better understanding. So this workaround does not work in my case. |
Simpler repro: type A = { kind: 'a' };
type B = { kind: 'b' };
declare const a: A;
declare const b: B;
declare function fab(arg: A | B): void;
declare function foo<T>(x: { kind: T }, f: (arg: { kind: T }) => void): void;
foo(a, fab); // Ok
foo(b, fab); // Error, but shouldn't be The issue here is that we make two contra-variant inferences for What we really should do is check if the co-variant inference is a subtype of any of the contra-variant inferences, and, if so, go with the co-variant inference. The random picking should really be a last resort since we know it's going to lead to an error downstream (it's still preferable to intersecting the contra-variant inferences because that'll produce a |
Bug Report
🔎 Search Terms
complex types generic inference
🕗 Version & Regression Information
TS Version: ^4.2
⏯ Playground Link
Playground link with relevant code
💻 Code
The text was updated successfully, but these errors were encountered: