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
[Nuxt/useLocalStorage]: Using useLocalStoage as state and hydrate function, TS2322: Type 'RemovableRef<{}>' is not assignable to type "xxx" or lost reactivity.
#2086
Closed
AnzhiZhang opened this issue
Mar 20, 2023
· 1 comment
In other words, only the options API will have the issue, the composition API is fine.
Did I write a wrong type of the state?
No I'm not. Remove the type and use same init value in the state option, the issue still exists. (There is a commented line in the reproduction)
Try to useLocalStorage().value
The useLocalStorage returns a Ref, so assign it's value property to the state could work (also a commented line).
But the reactivity will be lost. Launch the reproduction and open a new tab of it, use F12 to view and edit the Local Storages, and try to change the Local Storages or the input element, you will see the reactivity by useLocalStorage. This reactivity will not be lost when using useLocalStorage().value to avoid this typescript error.
An attempt to fin the reason
Since only hydrate has this problem, I noticed hydrate's first argument is UnwrapRef<S>, and this S is StateTree.
Reproduction
https://stackblitz.com/edit/github-1bnqyz?file=stores/counter.ts
Steps to reproduce the bug
n
, its type isnumber | undefined
.hydrate
function to set valueuseLocalStorage('storage', 0)
to it (IMPORTANT: notskipHydrate
)Expected behavior
Recognize the correct type of it.
Actual behavior
Type error.
Additional information
skipHydrate
This issue will only work on
hydrate
function instead ofskipHydrate
, I created another store namedexample.ts
. It works fine as in the document https://pinia.vuejs.org/cookbook/composables.html#ssr.In other words, only the options API will have the issue, the composition API is fine.
Did I write a wrong type of the
state
?No I'm not. Remove the type and use same init value in the state option, the issue still exists. (There is a commented line in the reproduction)
Try to useLocalStorage().value
The
useLocalStorage
returns a Ref, so assign it'svalue
property to the state could work (also a commented line).But the reactivity will be lost. Launch the reproduction and open a new tab of it, use F12 to view and edit the Local Storages, and try to change the Local Storages or the input element, you will see the reactivity by
useLocalStorage
. This reactivity will not be lost when usinguseLocalStorage().value
to avoid this typescript error.An attempt to fin the reason
Since only
hydrate
has this problem, I noticed hydrate's first argument isUnwrapRef<S>
, and thisS
isStateTree
.pinia/packages/pinia/src/types.ts
Lines 619 to 680 in efe11b1
The text was updated successfully, but these errors were encountered: