-
Notifications
You must be signed in to change notification settings - Fork 12.8k
Signatures of intersection/union types properties #9239
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
let second : A|B;
second.f = n; // Ops, no error! But it should be at least string&number here
second.f = str;// Ops, no error! But it should be at least string&number here
n = second.f; // error, typescript prefer to use first signature string. It should be string|number
str = second.f;// Ops, no error, typescript prefer to use first signature string. It should be string|number
let first : A&B;
first.f = n; // error, as f has type number&string. Probably OK, but probably it should be number|string, to be symmetric with set_f function. How would the language know that n = first.get_f(); // error, typescript prefer to use first signature () => string. Shouldn't it be string&number. The spec indicates that this behavior is correct, but I find it very counter-intuitive. |
I understand, that current behavior is correct according to spec, but in given cases it's cont-intuitive and gives you an opportunity to make an error that type-system could find.
Typescript found two overloads here. Really, it behaves same, as if it has signature
It will be really hard to calculate correct sum of all overrides, and it's OK that it's not working. But let's try to calculate it in this simplest case: Now let's look on result type:
TypeScript greatly infer correct result type here, as
Technically it's not correct: as Now let's look on properties. Compiler for sure can't have any knowledge that |
I think that the way that property types are inferred in an intersection is intuitive, but that the way that nullary function types are inferred is not. The problem is, as you say,
However, when intersecting the types of nullary functions the overload set produced is really counter intuitive because they are distinguished solely by return type. So the type |
We're continuing to look for a good algorithm that can produce "correct" signatures from intersection types. |
Hi there! Any progress here? |
Hey all - I just wanted to add a simplified example of where this is currently failing for me as well. |
Consider next example:
With #9167 additional to previous was added preserving readonly flag for intersection. Should't readonly flag be reset if any of intersection member has no such flag? As intersection should extend possibilities of type, and never narrow down it.
The text was updated successfully, but these errors were encountered: