import { createPinia, defineStore, setActivePinia } from '../src'
-import { computed, nextTick, watch } from 'vue'
+import { computed, nextTick, ref, watch } from 'vue'
describe('State', () => {
const useStore = () => {
await nextTick()
expect(spy).toHaveBeenCalledTimes(1)
})
+
+ it('unwraps refs', () => {
+ const name = ref('Eduardo')
+ const counter = ref(0)
+ const double = computed({
+ get: () => counter.value * 2,
+ set(val) {
+ counter.value = val / 2
+ },
+ })
+
+ setActivePinia(createPinia())
+ const useStore = defineStore({
+ id: 'main',
+ state: () => ({
+ name,
+ counter,
+ double,
+ }),
+ })
+
+ const store = useStore()
+
+ expect(store.name).toBe('Eduardo')
+ name.value = 'Ed'
+ expect(store.name).toBe('Ed')
+ store.name = 'Edu'
+ expect(store.name).toBe('Edu')
+ })
})
provide,
DebuggerEvent,
WatchOptions,
+ UnwrapRef,
} from 'vue'
import {
StateTree,
subscriptions.forEach((callback) => {
callback(
{ storeName: $id, type, payload: partialState, events: debuggerEvents },
- pinia.state.value[$id]
+ pinia.state.value[$id] as UnwrapRef<S>
)
})
}
}
}
const stopWatcher = watch(
- () => pinia.state.value[$id],
+ () => pinia.state.value[$id] as UnwrapRef<S>,
(state, oldState) => {
if (isListening) {
// TODO: remove payload
-import { DebuggerEvent } from 'vue'
+import { DebuggerEvent, UnwrapRef } from 'vue'
import { Pinia } from './rootStore'
/**
*/
events?: DebuggerEvent[] | DebuggerEvent
- payload: DeepPartial<S>
+ payload: DeepPartial<UnwrapRef<S>>
},
- state: S
+ state: UnwrapRef<S>
) => void
/**
/**
* State of the Store. Setting it will replace the whole state.
*/
- $state: S
+ $state: UnwrapRef<S>
/**
* Private property defining the pinia the store is attached to.
// has the actions without the context (this) for typings
A
> = StoreWithState<Id, S> &
- S &
+ UnwrapRef<S> &
StoreWithGetters<G> &
StoreWithActions<A> &
PiniaCustomProperties<Id, S, G, A>
*/
export type GettersTree<S extends StateTree> = Record<
string,
- ((state: S) => any) | (() => any)
+ ((state: UnwrapRef<S>) => any) | (() => any)
>
/**
/**
* Optional object of getters.
*/
- getters?: G & ThisType<S & StoreWithGetters<G> & PiniaCustomProperties>
+ getters?: G &
+ ThisType<UnwrapRef<S> & StoreWithGetters<G> & PiniaCustomProperties>
/**
* Optional object of actions.
*/
actions?: A &
ThisType<
A &
- S &
+ UnwrapRef<S> &
StoreWithState<Id, S> &
StoreWithGetters<G> &
PiniaCustomProperties
--- /dev/null
+import { computed, ref } from 'vue'
+import { defineStore, expectType } from './'
+
+const name = ref('Eduardo')
+const counter = ref(0)
+const double = computed({
+ get: () => counter.value * 2,
+ set(val) {
+ counter.value = val / 2
+ },
+})
+
+const useStore = defineStore({
+ id: 'name',
+ state: () => ({
+ n: 0,
+ name,
+ double,
+ counter,
+ }),
+
+ getters: {
+ myDouble: (state) => {
+ expectType<number>(state.double)
+ expectType<number>(state.counter)
+ return state.n * 2
+ },
+ other(): undefined {
+ expectType<number>(this.double)
+ expectType<number>(this.counter)
+ return undefined
+ },
+ },
+
+ actions: {
+ some() {
+ expectType<number>(this.$state.double)
+ expectType<number>(this.$state.counter)
+ expectType<number>(this.double)
+ expectType<number>(this.counter)
+ },
+ },
+})
+
+const store = useStore()
+
+expectType<number>(store.$state.counter)
+expectType<number>(store.$state.double)