1- import { createApp , customRef , isReactive , isRef , toRaw , triggerRef } from 'vue'
1+ import { computed , createApp , isReactive , isRef , toRaw , triggerRef } from 'vue'
22import type { App , ComputedRef , WritableComputedRef } from 'vue'
33import {
44 Pinia ,
99 _DeepPartial ,
1010 PiniaPluginContext ,
1111} from 'pinia'
12+ // NOTE: the implementation type is correct and contains up to date types
13+ // while the other types hide internal properties
14+ import type { ComputedRefImpl } from '@vue/reactivity'
1215
1316export interface TestingOptions {
1417 /**
@@ -206,7 +209,7 @@ function isPlainObject(
206209
207210function isComputed < T > (
208211 v : ComputedRef < T > | WritableComputedRef < T > | unknown
209- ) : v is ComputedRef < T > | WritableComputedRef < T > {
212+ ) : v is ( ComputedRef < T > | WritableComputedRef < T > ) & ComputedRefImpl < T > {
210213 return ! ! v && isRef ( v ) && 'effect' in v
211214}
212215
@@ -215,36 +218,33 @@ function WritableComputed({ store }: PiniaPluginContext) {
215218 for ( const key in rawStore ) {
216219 const originalComputed = rawStore [ key ]
217220 if ( isComputed ( originalComputed ) ) {
218- const originalFn = originalComputed . effect . fn
219- rawStore [ key ] = customRef ( ( track , trigger ) => {
220- // override the computed with a new one
221- const overriddenFn = ( ) =>
222- // @ts -expect-error: internal value
223- originalComputed . _value
224- // originalComputed.effect.fn = overriddenFn
225- return {
226- get : ( ) => {
227- track ( )
228- return originalComputed . value
229- } ,
230- set : ( newValue ) => {
231- // reset the computed to its original value by setting it to its initial state
232- if ( newValue === undefined ) {
233- originalComputed . effect . fn = originalFn
234- // @ts -expect-error: private api to remove the current cached value
235- delete originalComputed . _value
236- // @ts -expect-error: private api to force the recomputation
237- originalComputed . _dirty = true
238- } else {
239- originalComputed . effect . fn = overriddenFn
240- // @ts -expect-error: private api
241- originalComputed . _value = newValue
242- }
243- // this allows to trigger the original computed in setup stores
244- triggerRef ( originalComputed )
245- trigger ( )
246- } ,
247- }
221+ const originalFn = originalComputed . fn
222+ // override the computed with a new one
223+ const overriddenFn = ( ) =>
224+ // @ts -expect-error: internal cached value
225+ originalComputed . _value
226+ // originalComputed.fn = overriddenFn
227+
228+ rawStore [ key ] = computed < unknown > ( {
229+ get ( ) {
230+ return originalComputed . value
231+ } ,
232+ set ( newValue ) {
233+ // reset the computed to its original value by setting it to its initial state
234+ if ( newValue === undefined ) {
235+ originalComputed . fn = originalFn
236+ // @ts -expect-error: private api to remove the current cached value
237+ delete originalComputed . _value
238+ // @ts -expect-error: private api to force the recomputation
239+ originalComputed . _dirty = true
240+ } else {
241+ originalComputed . fn = overriddenFn
242+ // @ts -expect-error: private api
243+ originalComputed . _value = newValue
244+ }
245+ // this allows to trigger the original computed in setup stores
246+ triggerRef ( originalComputed )
247+ } ,
248248 } )
249249 }
250250 }
0 commit comments