Thanks to visit codestin.com
Credit goes to github.com

Skip to content

Commit bea3184

Browse files
authored
fix(reactiveComputed): computed fn should be ComputedGetter (#4528)
1 parent 570f18d commit bea3184

File tree

2 files changed

+26
-2
lines changed

2 files changed

+26
-2
lines changed

packages/shared/reactiveComputed/index.test.ts

Lines changed: 24 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -53,4 +53,28 @@ describe('reactiveComputed', () => {
5353
expect(dummy).toBe(1)
5454
expect(type).toBe('foo')
5555
})
56+
57+
it('should allow for previous value access (for vue 3.4+)', () => {
58+
const test = ref(0)
59+
60+
const obj = reactiveComputed<{ test: number, num: number }>(
61+
prev => ({ test: test.value, num: (prev?.num ?? 2) }),
62+
)
63+
64+
obj.num = 5
65+
expect(obj).toMatchInlineSnapshot(`
66+
{
67+
"num": 5,
68+
"test": 0,
69+
}
70+
`)
71+
72+
test.value = 1
73+
expect(obj).toMatchInlineSnapshot(`
74+
{
75+
"num": 5,
76+
"test": 1,
77+
}
78+
`)
79+
})
5680
})
Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,10 +1,10 @@
1-
import type { UnwrapNestedRefs } from 'vue'
1+
import type { ComputedGetter, UnwrapNestedRefs } from 'vue'
22
import { computed } from 'vue'
33
import { toReactive } from '../toReactive'
44

55
/**
66
* Computed reactive object.
77
*/
8-
export function reactiveComputed<T extends object>(fn: () => T): UnwrapNestedRefs<T> {
8+
export function reactiveComputed<T extends object>(fn: ComputedGetter<T>): UnwrapNestedRefs<T> {
99
return toReactive(computed(fn))
1010
}

0 commit comments

Comments
 (0)