You signed in with another tab or window. Reload to refresh your session.You signed out in another tab or window. Reload to refresh your session.You switched accounts on another tab or window. Reload to refresh your session.Dismiss alert
This pattern occurs when dog cannot always be defined when SomeComponent is mounted.
The problem is that even after I narrow dog's type to a non-nullable one, I have to add a non-null assertion to newDog.
If Dog has a large definition and the update is verbose, I have to put exclamation marks every time I refer to newDog in updateDogcallback.
However, when newDog is null, any update I would try is meaningless, while returning the new Dog object like in New dog! button's onClickhandler is the only valid and expected action.
So I think that removing the nullability of the state type is doable for convenience.
The current definition of useImmer reads as follows.
NonNullable<S> makes a generic assumption which cannot be guaranteed, that would apply to all users of useImmer. Instead, I recommend to indeed make your code type safe by leveraging TS's inference, e.g. updateDog(draft => { if (draft) { / * draft will be infered to be non-nullable here */ }) is barely more verbose and actually type safe against programmer errors.
Sometimes I use nullable state with
useImmer
, like following example.This pattern occurs when
dog
cannot always be defined whenSomeComponent
is mounted.The problem is that even after I narrow
dog
's type to a non-nullable one, I have to add a non-null assertion tonewDog
.If
Dog
has a large definition and the update is verbose, I have to put exclamation marks every time I refer tonewDog
inupdateDog
callback.However, when
newDog
isnull
, any update I would try is meaningless, while returning the newDog
object like inNew dog!
button'sonClick
handler is the only valid and expected action.So I think that removing the nullability of the state type is doable for convenience.
The current definition of
useImmer
reads as follows.What about changing it to the following one?
The text was updated successfully, but these errors were encountered: