Skip to content

Commit c6cd6a7

Browse files
authored
fix (types): do not unwrap refs in toRefs (#4966)
1 parent f2d2d7b commit c6cd6a7

File tree

2 files changed

+38
-38
lines changed

2 files changed

+38
-38
lines changed

packages/reactivity/src/ref.ts

+1-3
Original file line numberDiff line numberDiff line change
@@ -190,9 +190,7 @@ export function customRef<T>(factory: CustomRefFactory<T>): Ref<T> {
190190
}
191191

192192
export type ToRefs<T = any> = {
193-
// #2687: somehow using ToRef<T[K]> here turns the resulting type into
194-
// a union of multiple Ref<*> types instead of a single Ref<* | *> type.
195-
[K in keyof T]: T[K] extends Ref ? T[K] : Ref<UnwrapRef<T[K]>>
193+
[K in keyof T]: ToRef<T[K]>
196194
}
197195
export function toRefs<T extends object>(object: T): ToRefs<T> {
198196
if (__DEV__ && !isProxy(object)) {

test-dts/ref.test-d.ts

+37-35
Original file line numberDiff line numberDiff line change
@@ -10,8 +10,7 @@ import {
1010
toRef,
1111
toRefs,
1212
ToRefs,
13-
shallowReactive,
14-
watch
13+
shallowReactive
1514
} from './index'
1615

1716
function plainType(arg: number | Ref<number>) {
@@ -185,28 +184,45 @@ const p2 = proxyRefs(r2)
185184
expectType<number>(p2.a)
186185
expectType<Ref<string>>(p2.obj.k)
187186

188-
// toRef
189-
const obj = {
190-
a: 1,
191-
b: ref(1)
192-
}
193-
expectType<Ref<number>>(toRef(obj, 'a'))
194-
expectType<Ref<number>>(toRef(obj, 'b'))
187+
// toRef and toRefs
188+
{
189+
const obj: {
190+
a: number
191+
b: Ref<number>
192+
c: number | string
193+
} = {
194+
a: 1,
195+
b: ref(1),
196+
c: 1
197+
}
195198

196-
const objWithUnionProp: { a: string | number } = {
197-
a: 1
198-
}
199+
// toRef
200+
expectType<Ref<number>>(toRef(obj, 'a'))
201+
expectType<Ref<number>>(toRef(obj, 'b'))
202+
// Should not distribute Refs over union
203+
expectType<Ref<number | string>>(toRef(obj, 'c'))
204+
205+
// toRefs
206+
expectType<{
207+
a: Ref<number>
208+
b: Ref<number>
209+
// Should not distribute Refs over union
210+
c: Ref<number | string>
211+
}>(toRefs(obj))
212+
213+
// Both should not do any unwrapping
214+
const someReactive = shallowReactive({
215+
a: {
216+
b: ref(42)
217+
}
218+
})
199219

200-
watch(toRef(objWithUnionProp, 'a'), value => {
201-
expectType<string | number>(value)
202-
})
220+
const toRefResult = toRef(someReactive, 'a')
221+
const toRefsResult = toRefs(someReactive)
203222

204-
// toRefs
205-
const objRefs = toRefs(obj)
206-
expectType<{
207-
a: Ref<number>
208-
b: Ref<number>
209-
}>(objRefs)
223+
expectType<Ref<number>>(toRefResult.value.b)
224+
expectType<Ref<number>>(toRefsResult.a.value.b)
225+
}
210226

211227
// #2687
212228
interface AppData {
@@ -238,20 +254,6 @@ function testUnrefGenerics<T>(p: T | Ref<T>) {
238254

239255
testUnrefGenerics(1)
240256

241-
// #4732
242-
describe('ref in shallow reactive', () => {
243-
const baz = shallowReactive({
244-
foo: {
245-
bar: ref(42)
246-
}
247-
})
248-
249-
const foo = toRef(baz, 'foo')
250-
251-
expectType<Ref<number>>(foo.value.bar)
252-
expectType<number>(foo.value.bar.value)
253-
})
254-
255257
// #4771
256258
describe('shallow reactive in reactive', () => {
257259
const baz = reactive({

0 commit comments

Comments
 (0)