@@ -2,7 +2,6 @@ import { Data } from '../component'
2
2
import { RefKey , ReadonlyIdentifierKey } from '../utils/symbols'
3
3
import { proxy , isPlainObject , warn } from '../utils'
4
4
import { reactive , isReactive , shallowReactive } from './reactive'
5
- import { ComputedRef } from '../apis/computed'
6
5
7
6
declare const _refBrand : unique symbol
8
7
export interface Ref < T = any > {
@@ -22,16 +21,18 @@ type WeakCollections = WeakMap<any, any> | WeakSet<any>
22
21
// RelativePath extends object -> true
23
22
type BaseTypes = string | number | boolean | Node | Window
24
23
25
- export type UnwrapRef < T > = T extends ComputedRef < infer V >
26
- ? UnwrapRefSimple < V >
27
- : T extends Ref < infer V >
24
+ export type ShallowUnwrapRef < T > = {
25
+ [ K in keyof T ] : T [ K ] extends Ref < infer V > ? V : T [ K ]
26
+ }
27
+
28
+ export type UnwrapRef < T > = T extends Ref < infer V >
28
29
? UnwrapRefSimple < V >
29
30
: UnwrapRefSimple < T >
30
31
31
32
type UnwrapRefSimple < T > = T extends Function | CollectionTypes | BaseTypes | Ref
32
33
? T
33
34
: T extends Array < any >
34
- ? T
35
+ ? { [ K in keyof T ] : UnwrapRefSimple < T [ K ] > }
35
36
: T extends object
36
37
? UnwrappedObject < T >
37
38
: T
@@ -50,6 +51,7 @@ type SymbolExtract<T> = (T extends { [Symbol.asyncIterator]: infer V }
50
51
: { } ) &
51
52
( T extends { [ Symbol . iterator ] : infer V } ? { [ Symbol . iterator ] : V } : { } ) &
52
53
( T extends { [ Symbol . match ] : infer V } ? { [ Symbol . match ] : V } : { } ) &
54
+ ( T extends { [ Symbol . matchAll ] : infer V } ? { [ Symbol . matchAll ] : V } : { } ) &
53
55
( T extends { [ Symbol . replace ] : infer V } ? { [ Symbol . replace ] : V } : { } ) &
54
56
( T extends { [ Symbol . search ] : infer V } ? { [ Symbol . search ] : V } : { } ) &
55
57
( T extends { [ Symbol . species ] : infer V } ? { [ Symbol . species ] : V } : { } ) &
@@ -188,3 +190,31 @@ export function triggerRef(value: any) {
188
190
189
191
value . value = value . value
190
192
}
193
+
194
+ export function proxyRefs < T extends object > (
195
+ objectWithRefs : T
196
+ ) : ShallowUnwrapRef < T > {
197
+ if ( isReactive ( objectWithRefs ) ) {
198
+ return objectWithRefs as ShallowUnwrapRef < T >
199
+ }
200
+ const value : Record < string , any > = reactive ( { [ RefKey ] : objectWithRefs } )
201
+
202
+ for ( const key of Object . keys ( objectWithRefs ) ) {
203
+ proxy ( value , key , {
204
+ get ( ) {
205
+ if ( isRef ( value [ key ] ) ) {
206
+ return value [ key ] . value
207
+ }
208
+ return value [ key ]
209
+ } ,
210
+ set ( v : unknown ) {
211
+ if ( isRef ( value [ key ] ) ) {
212
+ return ( value [ key ] . value = unref ( v ) )
213
+ }
214
+ value [ key ] = unref ( v )
215
+ } ,
216
+ } )
217
+ }
218
+
219
+ return value as ShallowUnwrapRef < T >
220
+ }
0 commit comments