Closed chrbala closed 2 years ago
I agree. I've made the change. Thanks for the idea!
It looks like the change typed it as [unknown, UpdateState<unknown>]
, but actually it's valid to type it as [unknown, UpdateState<T>]
. That will enforce the types going in to the state, but will not guarantee the types coming out.
Hmm. But you don't know the types going into the state. My understanding is that there isn't a practical difference between UpdateState<unknown>
and UpdateState<T>
because of the other overrides that capture the other cases where we know the type. Am I wrong? Can you show an example where there is a difference?
Ah, looks like you're right, I missed that. Thanks!
The
value
in[value, setValue] = useLocalStorageState('key')
can be updated outside of the closure: dev tools, different app versions, different parts of the app with the same key, etc. To avoid runtime errors,value
should therefore be typed asunknown
so that assertions (or coercions) are explicitly made before treating the output type the same as the input type.