-
-
Notifications
You must be signed in to change notification settings - Fork 46
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
Add basic narrowing #156
Add basic narrowing #156
Conversation
- Currently basic equality
- Add narrowing for `typeof` - Add narrowing for dependent booleans
- Change to collect map before hand - Add negation or filtering - Add conjugtions - Add disjoint checking to equality to fix `??` == undefined issue - Improve LocalInformation merging around final events
- cargo fmt - fix clippy lints - remove disjoint operator checking (will add back in #186)
This comment was marked as resolved.
This comment was marked as resolved.
- Prototypes with `instanceof` - New expression - General refactors
- Add `Filter` helper - Property access narrowing - Add De Morgan's laws for narrowing && and || - Add asserts return type checking - Add narrowing lookup for conditional result for printing and access
into.insert(on, types.new_narrowed(on, narrowed_to)); | ||
} | ||
constructor => { | ||
if let Some((lhs, rhs)) = as_logical_and(constructor, types) { |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
@danvk thought you might find this interesting! I watched the Michigan TypeScript video and read your blog post on implementing inferred type predicates. (congrats on getting it merged, it is a great feature!)
In Ezno it was a lot easier to add narrowing with its more dependent type system. In Ezno the expression typeof x === "number"
has a type like EqOperator<TypeOfOperator<typeof x>, "number">
which makes getting the information for narrowing pretty simple (you can sort of see that in the lines above). So Ezno has something closer to your original intuition rather than the TSC flow node control flow structure.
One of the interesting parts was "Pathological Cases and an Insight" with this case edge case in the else
function flakyIsStringRewrite(x: string | null) {
if (typeof x === 'string' && Math.random() > 0.5) {
x; // type is string
} else {
x; // type is string | null
}
}
this helped me fix a bug in my implementation. For else branches I run get_narrowed_values(condition, true)
, where true
negates the condition in the if
. If I have the &&
expression above with negate = true
it then means that it applies one of the De Morgans law. So it now treats !(a && b)
in the code as !a || !b
and it only adds a narrow if both sides have a condition for the same variable (=on
here) (the zipping code being for (on, lhs_request) in lhs_requests { if let Some(rhs_request) = rhs_requests.get(&on) { ...
).
I think I have got this covered in the tests here
Don't know if there is an equivalent section in the TS narrowing logic but thought my intuition thinking about De Morgan's laws and generating A | B
only if both sides of an ||
condition give narrowed values for the variable was a nice way to handle this case.
- Fix test - Type::Narrowed *from* calling fix - Narrowing from free variable
Most definitely missing some functionality but the basics are in so merging 🎉 |
Adds narrowing of variable reference types based on JavaScript equality constructs.
Currently adds the following
===
narrowingtypeof
narrowing&&
and||
assert *value* is *type*
checking(X | Y).tag === ...
*key* in obj
case removal and creationinstanceof
Support across variable declarations and functions thanks to more dependent types