@@ -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 ,
@@ -316,7 +317,7 @@ export function toRefs<T extends object>(object: T): ToRefs<T> {
316
317
}
317
318
const ret : any = isArray ( object ) ? new Array ( object . length ) : { }
318
319
for ( const key in object ) {
319
- ret [ key ] = toRef ( object , key )
320
+ ret [ key ] = propertyToRef ( object , key )
320
321
}
321
322
return ret
322
323
}
@@ -409,20 +410,24 @@ export function toRef(
409
410
return source
410
411
} else if ( isFunction ( source ) ) {
411
412
return new GetterRefImpl ( source as ( ) => unknown ) as any
412
- } else if ( isPlainObject ( source ) && key ) {
413
- const val = ( source as Record < string , any > ) [ key ]
414
- return isRef ( val )
415
- ? val
416
- : ( new ObjectRefImpl (
417
- source as Record < string , any > ,
418
- key ,
419
- defaultValue
420
- ) as any )
413
+ } else if ( isObject ( source ) && isString ( key ) ) {
414
+ return propertyToRef ( source , key , defaultValue )
421
415
} else {
422
416
return ref ( source )
423
417
}
424
418
}
425
419
420
+ function propertyToRef ( source : object , key : string , defaultValue ?: unknown ) {
421
+ const val = ( source as any ) [ key ]
422
+ return isRef ( val )
423
+ ? val
424
+ : ( new ObjectRefImpl (
425
+ source as Record < string , any > ,
426
+ key ,
427
+ defaultValue
428
+ ) as any )
429
+ }
430
+
426
431
// corner case when use narrows type
427
432
// Ex. type RelativePath = string & { __brand: unknown }
428
433
// RelativePath extends object -> true
0 commit comments