-
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
Type predicate calling another predicate with different argument position #6311
Comments
@DanielRosenwasser - This is a bug. And by "bug" I mean "unaccounted for case in our widening logic". We need to make predicate return types get widened so the predicates don't get compared. Should probably also add a test case for this. |
So by "a bug", you mean "a bug". 😄 |
@weswigham There's a couple of places that we need to widen type predicates that we don't: declare function isString1(a: number, b: Object): b is string;
declare function isString2(a: Object): a is string;
switch (isString1(0, "")) {
case isString2(""): // error
}
var x = isString1(0, "") === isString2(""); // error I'd like to hear opinions from others on how we can mitigate this. Is it feasible to widen at all locations where we need to relate types? @mhegazy @JsonFreeman |
@DanielRosenwasser |
Oh no... This is an example of why I was so against having type predicate types. It is not good to widen all places where we compare types. If that happens, then any place where we do a subtype comparison (type argument inference, subtype reduction, etc), you will have null and undefined widening to any, and then dominating other types in the comparison. I can think of 3 options, so as usual, here comes a list:
If I understand correctly, widening is more about preventing the type predicate from "landing" on a variable. That is not the issue in this case, as there are no variables involved. |
TBH, I think the part of the type relationship which checks predicate index compatibility just needs to be moved form the predicate relationship code into the signature relationship code, since its only relevant during signature comparison. |
…ture comparison
This approach is similar to option 2 I've mentioned above. |
This should be fixed. Give it a shot in our nightlies. Thanks @andy-hanson! |
This code will not compile:
Output:
The code will work if I swap the parameters of
isString1
so that the predicated parameter for both functions is the first argument.Is there a good reason that this is required?
The text was updated successfully, but these errors were encountered: