@@ -11,7 +11,8 @@ import {
11
11
hasChanged ,
12
12
IfAny ,
13
13
isFunction ,
14
- isPlainObject
14
+ isString ,
15
+ isObject
15
16
} from '@vue/shared'
16
17
import {
17
18
isProxy ,
@@ -219,7 +220,7 @@ export function toRefs<T extends object>(object: T): ToRefs<T> {
219
220
}
220
221
const ret : any = isArray ( object ) ? new Array ( object . length ) : { }
221
222
for ( const key in object ) {
222
- ret [ key ] = toRef ( object , key )
223
+ ret [ key ] = propertyToRef ( object , key )
223
224
}
224
225
return ret
225
226
}
@@ -285,20 +286,24 @@ export function toRef(
285
286
return source
286
287
} else if ( isFunction ( source ) ) {
287
288
return new GetterRefImpl ( source as ( ) => unknown ) as any
288
- } else if ( isPlainObject ( source ) && key ) {
289
- const val = ( source as Record < string , any > ) [ key ]
290
- return isRef ( val )
291
- ? val
292
- : ( new ObjectRefImpl (
293
- source as Record < string , any > ,
294
- key ,
295
- defaultValue
296
- ) as any )
289
+ } else if ( isObject ( source ) && isString ( key ) ) {
290
+ return propertyToRef ( source , key , defaultValue )
297
291
} else {
298
292
return ref ( source )
299
293
}
300
294
}
301
295
296
+ function propertyToRef ( source : object , key : string , defaultValue ?: unknown ) {
297
+ const val = ( source as any ) [ key ]
298
+ return isRef ( val )
299
+ ? val
300
+ : ( new ObjectRefImpl (
301
+ source as Record < string , any > ,
302
+ key ,
303
+ defaultValue
304
+ ) as any )
305
+ }
306
+
302
307
// corner case when use narrows type
303
308
// Ex. type RelativePath = string & { __brand: unknown }
304
309
// RelativePath extends object -> true
0 commit comments