Skip to content
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

Merged
merged 11 commits into from
Aug 20, 2024
Merged

Add basic narrowing #156

merged 11 commits into from
Aug 20, 2024

Conversation

kaleidawave
Copy link
Owner

@kaleidawave kaleidawave commented Jun 3, 2024

Adds narrowing of variable reference types based on JavaScript equality constructs.

Currently adds the following

  • === narrowing
  • typeof narrowing
  • Support for && and ||
  • De Morgans's law edge cases
  • assert *value* is *type* checking
  • Some number extras which require additions from Basic operator checking #186
  • (X | Y).tag === ...
  • *key* in obj case removal and creation
  • instanceof

Support across variable declarations and functions thanks to more dependent types

Additionally the LocalInformation merging now handles final events better. (see new control flow test, which would previously be 3 | 7) AND AnonomousObjectLiteral stores the properties on the type

- Currently basic equality
@kaleidawave kaleidawave added enhancement New feature or request checking Issues around checking labels Jun 3, 2024
@kaleidawave kaleidawave linked an issue Jun 3, 2024 that may be closed by this pull request
@kaleidawave kaleidawave mentioned this pull request Jul 21, 2024
- 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)
@kaleidawave

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
@kaleidawave kaleidawave added the narrowing Expressions that change the types of values label Aug 20, 2024
@kaleidawave kaleidawave changed the title Add basic narrowing #122 Add basic narrowing Aug 20, 2024
into.insert(on, types.new_narrowed(on, narrowed_to));
}
constructor => {
if let Some((lhs, rhs)) = as_logical_and(constructor, types) {
Copy link
Owner Author

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
@kaleidawave kaleidawave marked this pull request as ready for review August 20, 2024 20:33
@kaleidawave
Copy link
Owner Author

Most definitely missing some functionality but the basics are in so merging 🎉

@kaleidawave kaleidawave merged commit 793f54d into main Aug 20, 2024
9 checks passed
@kaleidawave kaleidawave deleted the narrowing branch August 20, 2024 20:40
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
checking Issues around checking enhancement New feature or request narrowing Expressions that change the types of values
Projects
None yet
Development

Successfully merging this pull request may close these issues.

Narrowing
1 participant