-
Notifications
You must be signed in to change notification settings - Fork 4.6k
Real-time collaboration: Improve type safety with YMapWrap #72808
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
Conversation
|
The following accounts have interacted with this PR and/or linked issues. I will continue to update these lists as activity occurs. You can also manually ask me to refresh this list by adding the If you're merging code through a pull request on GitHub, copy and paste the following into the bottom of the merge commit message. To understand the WordPress project's expectations around crediting contributors, please review the Contributor Attribution page in the Core Handbook. |
|
Flaky tests detected in 2ebf66f. 🔍 Workflow run URL: https://github.com/WordPress/gutenberg/actions/runs/18923171665
|
2ebf66f to
0cfb380
Compare
| export function isYMap< T extends YMapRecord >( | ||
| value: unknown | ||
| ): value is YMapWrap< T > { | ||
| return value instanceof Y.Map; | ||
| } |
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.
Should we have any concern that this type guard could get used by someone with the assumption that it somehow is doing a check for the actual type T when you can actually just pass any T type and as long as it's a Y.Map it will return true? Should we give it a name that indicates it isn't actually checking the generic type?
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.
Generic types are never checked. It's impossible to do run-time type checks in a type. They are always just pass-through. Open to another name if you think this isn't clear
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.
Thanks for the good question, I pushed up 2d2e90b which makes it a much stricter type guard
What?
Improve the type safety of
Y.Map.Why?
The typings for
Y.Maphave limitations. By default, the type ofY.Mapvalues is the union of all possible values of the map. This type is accurate, but its non-specificity requires aggressive type narrowing or type casting / destruction withas—usually the latter. Preventable bugs sneak in.How?
This PR introduces
YMapWrap, which can infer the correct value type based on the provided key. It is just a type overlay and does not change the runtime behavior ofY.Map. Using this type, we can remove almost all of uses ofasin the CRDT code.We also benefit from more clearly representing the data structure encoded in the CRDT document. See
YPostRecordandYBlockRecord.Testing Instructions
These changes are covered by existing tests, but you can check out this branch and confirm the added type safety by trying to introduce type bugs.
Before
After