Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

fix(reactiveComputed): unwrap reactive type #3215

Merged
merged 1 commit into from
Jul 30, 2023
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Jump to
Jump to file
Failed to load files.
Diff view
Diff view
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>
}