Skip to content

Commit

Permalink
fix(reactiveComputed): unwrap reactive type
Browse files Browse the repository at this point in the history
  • Loading branch information
sxzz committed Jul 30, 2023
1 parent 296dcc5 commit 67f4629
Show file tree
Hide file tree
Showing 3 changed files with 9 additions and 7 deletions.
5 changes: 3 additions & 2 deletions packages/shared/reactiveComputed/index.test.ts
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
import { isVue2, nextTick, ref, watch, watchEffect } from 'vue-demi'
import { describe, expect, it } from 'vitest'
import { describe, expect, expectTypeOf, it } from 'vitest'
import { reactiveComputed } from '.'

describe('reactiveComputed', () => {
Expand All @@ -8,9 +8,10 @@ describe('reactiveComputed', () => {

const state = reactiveComputed(() => {
return {
count: count.value,
count,
}
})
expectTypeOf(state).toEqualTypeOf<{ count: number }>()

expect(state.count).toBe(0)

Expand Down
3 changes: 2 additions & 1 deletion packages/shared/reactiveComputed/index.ts
Original file line number Diff line number Diff line change
@@ -1,9 +1,10 @@
import type { UnwrapNestedRefs } from 'vue-demi'
import { computed } from 'vue-demi'
import { toReactive } from '../toReactive'

/**
* Computed reactive object.
*/
export function reactiveComputed<T extends object>(fn: () => T): T {
export function reactiveComputed<T extends object>(fn: () => T): UnwrapNestedRefs<T> {
return toReactive(computed(fn))
}
8 changes: 4 additions & 4 deletions packages/shared/toReactive/index.ts
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
// eslint-disable-next-line no-restricted-imports
import { isRef, reactive, unref } from 'vue-demi'
import { type UnwrapNestedRefs, isRef, reactive, unref } from 'vue-demi'
import type { MaybeRef } from '../utils'

/**
Expand All @@ -10,9 +10,9 @@ import type { MaybeRef } from '../utils'
*/
export function toReactive<T extends object>(
objectRef: MaybeRef<T>,
): T {
): UnwrapNestedRefs<T> {
if (!isRef(objectRef))
return reactive(objectRef) as T
return reactive(objectRef)

const proxy = new Proxy({}, {
get(_, p, receiver) {
Expand Down Expand Up @@ -42,5 +42,5 @@ export function toReactive<T extends object>(
},
})

return reactive(proxy) as T
return reactive(proxy) as UnwrapNestedRefs<T>
}

0 comments on commit 67f4629

Please sign in to comment.