@@ -5,9 +5,9 @@ import { IS_PROXY, PROXY_SOURCES, PROXY_TARGET, RESOLVE_CONTEXT } from './consta
5
5
import { recompute } from './recompute'
6
6
import type { MergeContext , MergeSource , MergedObjectValue , PathSegments , ResolveContext } from '.'
7
7
8
- export const createProxy = ( context : MergeContext , target : MergeSource , resolveContext : ResolveContext , pathSegments : PathSegments = [ ] ) => {
9
- const handler = createHandler ( context , resolveContext , pathSegments )
10
- const proxy = markRaw ( new Proxy ( target , handler ) )
8
+ export const createProxy = < T extends Record < string , any > > ( context : MergeContext < T > , target : T , resolveContext : ResolveContext , pathSegments : PathSegments = [ ] ) : MergeSource < T > => {
9
+ const handler = createHandler < T > ( context , resolveContext , pathSegments )
10
+ const proxy = markRaw ( new Proxy ( target , handler ) ) as MergeSource < T >
11
11
12
12
if ( ! pathSegments . length && context . sources ) {
13
13
context . sources . push ( proxy )
@@ -16,7 +16,7 @@ export const createProxy = (context: MergeContext, target: MergeSource, resolveC
16
16
return proxy
17
17
}
18
18
19
- export const createHandler : ( context : MergeContext , resolveContext : ResolveContext , pathSegments : PathSegments ) => ProxyHandler < any > = ( context , resolveContext , pathSegments = [ ] ) => ( {
19
+ export const createHandler = < T > ( context : MergeContext < T > , resolveContext : ResolveContext , pathSegments : PathSegments = [ ] ) : ProxyHandler < MergeSource < T > > => ( {
20
20
get : ( target , key , receiver ) => {
21
21
if ( key === IS_PROXY ) {
22
22
return true
@@ -40,11 +40,11 @@ export const createHandler: (context: MergeContext, resolveContext: ResolveConte
40
40
return value
41
41
}
42
42
43
- if ( ! value [ IS_PROXY ] ) {
43
+ if ( ! ( value as MergeSource < T > ) [ IS_PROXY ] ) {
44
44
const keyPath : PathSegments = [ ...pathSegments , ( key as string ) ]
45
45
46
- value = createProxy ( context , value , resolveContext , keyPath )
47
- target [ key ] = value
46
+ value = createProxy < typeof value > ( context , value , resolveContext , keyPath )
47
+ Reflect . set ( target , key , value )
48
48
}
49
49
50
50
return value
@@ -87,8 +87,8 @@ export const createHandler: (context: MergeContext, resolveContext: ResolveConte
87
87
return success
88
88
}
89
89
90
- let keyContexts : Array < ResolveContext > = [ ]
91
- let keySources
90
+ let keyContexts : ResolveContext [ ] = [ ]
91
+ let keySources : MergeSource < T > [ ]
92
92
93
93
if ( isArrayItem ) {
94
94
keySources = proxies
@@ -138,6 +138,7 @@ export const createHandler: (context: MergeContext, resolveContext: ResolveConte
138
138
139
139
let index = 0
140
140
for ( const segment of pathSegments ) {
141
+ // @ts -ignore
141
142
proxies = proxies . map ( proxy => proxy [ segment ] )
142
143
143
144
if ( isArrayItem && index === pathSegments . length - 1 ) {
@@ -152,7 +153,7 @@ export const createHandler: (context: MergeContext, resolveContext: ResolveConte
152
153
// Check if the key still exists in one of the sourceProxies,
153
154
// if so resolve the new value, if not remove the key
154
155
if ( proxies . some ( proxy => ( key in proxy ) ) ) {
155
- let keyContexts : Array < ResolveContext > = [ ]
156
+ let keyContexts : ResolveContext [ ] = [ ]
156
157
let keySources
157
158
158
159
if ( isArrayItem ) {
0 commit comments