More Type-Safety (React MVVM Version 3 Proposal) #11
Andrei15193
announced in
Announcements
Replies: 0 comments
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
-
It has not been that long since I've published version 2.2.0 and it will take a while until I publish version 3. In the mean time I want to write a few of the changes I will be making to further improve the library.
Before going into the details of one of the changes, I want to publish a new major version to clean the API a little bit. There are a few deprecations. Besides this, some of the changes I want to make may introduce some breaking changes, none of them major, just some tidying up along the way.
Without further ado, I will describe one of the changes I will be making to the library.
More Type-Safety
As the title suggests, I want to further enhance the type checking features of the library, especially around notifying about changed properties.
What has been somewhat bothering me is that whenever I want to notify about which properties may have changed, I do not get a list from where I can pick correct values. If I rename a property, I do not get compilation errors about it or have the IDE perform the rename in this case as well.
To illustrate the issue, consider the following code snippet.
There is no compilation error, I can notify about anything.
Version 3 Proposed Change
This may introduce a breaking change, I want to change the method declaration for
notifyPropertiesChanged
to usekeyof
. Initially I tried to use it in combination withExtract<,>
to only select keys that are of typestring
, however this does not work with the compiler and I do not get the auto-complete or compilation checks that I wanted and thus decided to postpone this.After some thinking, using
keyof
directly is not such a bad idea. You get all the options and will need to pick the ones that make sense. Not only this will offer a simple interface, but can be extended to event handlers as well.This will have ripple effects on the
propertiesChanged
event as well, but since it goes from a list ofstring
values to a list ofkeyof
values everything should still work without any breaking changes.The only issues that can happen are compilation errors about inexistent properties.
Extending to Event Handlers
This will change event handlers as with this change we would need to use an IEventHandler<readonly (keyof this)[]> to get the benefits.
I want to introduce a few interfaces that simplify this.
This should help with readability and library exploration as well as getting the benefits of type checking into event handlers.
Inline Event Handlers
When using composition and event subscriptions, check ViewModel Design Considerations for more details, is providing an event handler that has more members than what the IEventHandler specifies.
Unfortunately, with what we currently have with React MVVM, the following code snippet will generate compilation errors.
This is not a huge issue, we can work around this using closures or defining classes that implement the interface, however it does take away from the flexibility as well as having to deal with little details when it comes to edge cases such as this.
To solve this, I will be making the
subscribe
andunsubscribe
methods generic.For some reason we get a compilation error although our object implements the interface and even exposes a few extra properties. This is not a problem if we would define a class implementing the interface and create an instance of it, but when we use object literals it acts differently.
With this change, the initial snippet that was giving compilation errors will now work and we will be able to use object literals for inline event handlers giving full flexibility on this is used.
Closing Thoughts
Now this can be a breaking change if I go with it, might change the
IEvent
andIEventHandler
interfaces even more.The issue is around the
sender
parameter when handing an event. Currently I've set it as anobject
which more or less translates toany
. While it is not bad, it is not great either.Generally, when dealing with "general" objects or objects whose value we do not know, we should use
unknown
instead ofany
because the former will generate compilation errors when trying to access any members while with the former it will just work.This will clearly indicate that the source of the event is
unknown
and we need to perform an explicit cast to handle it.I have not yet decided if I will have a typed
sender
object or just switch it tounknown
. I haven't really used this parameter much in event handlers (removing it is another option) making theunknown
approach more tempting.Beta Was this translation helpful? Give feedback.
All reactions