-
Notifications
You must be signed in to change notification settings - Fork 12.8k
inconsistency in type inference on union of function types #9104
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
It would be nice if string & number = never // type error All incomatible primitive types should produce This behavior would allow expressing the following function union type: type F1 = (a: A1, b: B1, c: C1) => R1
type F2 = (a: A2, b: B2) => R2
type F3 = F1 | F2; // (a: A1 & A2, b: B1 & B2, c: C1) => R1 | R2 thus string & (string | number) = (string & string) | (string & number) = string | never = string which allow assigning the correct type for the original issue. |
See #4612 |
Duplicate #10025 |
For the "real issue":
In addition I propose:
I know, the last could be seen to differ from how TypeScript handles function types in general, but it is closer to what ECMAScript allows:
In TypeScript:
I appreciate, that TypeScript requires the exact amount of parameters to be passed, but it shall be less picky in case of union function types. This allows us to simply run the method with two parameters - additional parameters are ignored if not required by the actual implementation. For the Intersection function type it should be the same, except for the return type:
The "VALUE DOMAIN"-Example from the initial post works in TS 2.0.3 as described, but in my opinion it IS wrong, that it compiles. See the following example:
I expect, that the resulting fn1 can only be invoked with an I1 as argument, but not an I2, as it may be of type (arg:I1)=>void at runtime, which the compiler can detect with the rules described above. |
TypeScript Version:
1.8.10 (seems, that other are affected as well)
Code
Expected behavior:
(arg: string) => string
and should not be dependent on the order of union members / conditional branches.Actual behavior:
The text was updated successfully, but these errors were encountered: