[Typescript] Define stores using a generic function #973
Replies: 2 comments 2 replies
-
I suspect the item you are passing to appendItem is a ref() , not a So either change the T type as Ref<T> everywhere if you want to store the ref, or change the type of item in appendItem to Ref<T> and read appendItem(item: Ref<T>) {
this.count++; // OK
this.items.push(item.value);
}, Just to be sure, comment out the line where you push the item to the array, and call Wishing you success and health. |
Beta Was this translation helpful? Give feedback.
-
I was wrong about ref using reactive behind the scenes. https://vuejs.org/guide/extras/reactivity-in-depth.html#how-reactivity-works-in-vue |
Beta Was this translation helpful? Give feedback.
Uh oh!
There was an error while loading. Please reload this page.
-
I'm experimenting with a generic function that can create a fully typed store.
In my actions, the
items
array is typed as anUnwrapRefSimple<T>[]
. This means that Typescript doesn't allow me to mutate the array with an item typedT
.If I change the code like this, Typescript no longer complains.
I highly doubt that this "solution" has 0 unintended side-effects.
Maybe using generics to create stores is an anti-pattern. Maybe it is more fitting for my use case to define each store separately. I'm just exploring my options.
If somebody could point out what I'm doing wrong, that would be great :D
Beta Was this translation helpful? Give feedback.
All reactions