-
-
Notifications
You must be signed in to change notification settings - Fork 2k
Allowing action.type to be Type<T> #168
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
Comments
Action.type
to be Type<T>
Apparently you don't get the same inferences in switch statements at all. I've filed a bug with typescript about it. |
So yeah, this won't work, of course. Stupid me, sorry. |
Going to close this. We are committed to keeping actions serializable. Thanks! |
👍 Thank you for this library. |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
Uh oh!
There was an error while loading. Please reload this page.
There's one issue that I raised that has I think has merit as a more limited proposal, and that is to use the declared type of an action, rather than insisting the user provide a string proxy.
Less code, more readable
In order to define an action, the type needs to be declared at least three different times.
If
Type<T>
was used instead, the definition would becomeReadability improvements are not limited to the action definition either, reducers have a slight improvement:
In general, any place that the string proxy
MY_ACTION
is used, it could be replaced with the more directMyAction
.The actions aren't serialisable
Two problems exist which prevent these actions from ever being serializable:
eval
of untrusted stringsMyAction !== deserialize(MyAction.toString())
), so it can't be matched in reducersThe notion of using a unserialisable key has been discussed elsewhere (#143), but it was noted that a clear motivation would be required. However, while I feel that the uniqueness of
Symbol
s does not provide a clear enough motivation,Type<T>
is similarly unique, already available as a tokenas part of the class declaration, and provides further benefits to type safety and readability.
Any feature which depends on the serialisability of actions could confine itself to using type strings exclusively.
The signature of
Actions.ofType
can be fixedImproving the type safety of effects today, rather than waiting for the typescript team to support binding the type via the typestring.
(which I'm still surprised to know is possible in switch statements).I'm still doubtful that binding the type by typestring at the callsite of(this is all nonsense).ofType
will ever be possible though. A switch statement knows that you're switching onaction.type
(and can bind appropriately), whereas there is no way for typescript to know that the internals of the functionofType
even refer toaction.type
, so even though the string at the callsite matches the readonlyaction.type
, it cannot make an inference of the type (a function is a lot more complex than aswitch
). But I was wrong about switch statements, so I might be wrong here too.Actions must be passed to devtools
If devtools tried to log
action.type
, the output might beMyAction.toString()
, rather than the preferableMyAction.name
. I don't know enough about devtools to know whether it is possible to ask devtools to use the better string.Update: Removed needless self-criticism from the proposal.
The text was updated successfully, but these errors were encountered: