Real-time collaboration: Improve type safety with YMapWrap
#73948
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
What?
Introduce a type-safe wrapper for
Y.Map.Why?
The build-in typings for
Y.Maphave limitations. By default, the type of Y.Map values is the union of all possible values of the map. Its non-specificity requires aggressive type narrowing or (usually) type casting / destruction withas.See #73946 for an example bug that can be prevented with stronger type safety.
How?
This PR introduces
YMapWrap, which can infer the correct value type based on the provided key. It is a type overlay and does not change the runtime behavior ofY.Map. Using this type, we can remove almost all of uses ofasin our CRDT-related 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
Testing Instructions for Keyboard
n/a