1
1
import {
2
+ IObjectDidChange ,
3
+ IObjectWillChange ,
2
4
_getAdministration ,
3
5
_interceptReads ,
4
6
action ,
5
7
computed ,
6
8
defineProperty ,
7
- intercept ,
8
9
getAtom ,
9
- IObjectWillChange ,
10
+ intercept ,
11
+ makeObservable ,
10
12
observable ,
11
13
observe ,
12
- set ,
13
- IObjectDidChange ,
14
- makeObservable
14
+ set
15
15
} from "mobx"
16
16
import {
17
- addHiddenFinalProp ,
18
- addHiddenWritableProp ,
17
+ AnyNode ,
18
+ AnyObjectNode ,
19
19
ArrayType ,
20
20
ComplexType ,
21
- createActionInvoker ,
22
- createObjectNode ,
23
21
EMPTY_ARRAY ,
24
22
EMPTY_OBJECT ,
25
- escapeJsonPath ,
23
+ FunctionWithFlag ,
24
+ Hook ,
25
+ IAnyType ,
26
+ IChildNodesMap ,
27
+ IJsonPatch ,
28
+ IType ,
29
+ IValidationContext ,
30
+ IValidationResult ,
31
+ Instance ,
32
+ MapType ,
26
33
MstError ,
34
+ TypeFlags ,
35
+ _CustomOrOther ,
36
+ _NotCustomized ,
37
+ addHiddenFinalProp ,
38
+ addHiddenWritableProp ,
39
+ assertArg ,
40
+ assertIsString ,
41
+ createActionInvoker ,
42
+ createObjectNode ,
43
+ devMode ,
44
+ escapeJsonPath ,
27
45
flattenTypeErrors ,
28
46
freeze ,
29
47
getContextForPath ,
30
48
getPrimitiveFactoryFromValue ,
31
49
getStateTreeNode ,
32
- IAnyType ,
33
- IChildNodesMap ,
34
- IValidationContext ,
35
- IJsonPatch ,
36
50
isPlainObject ,
37
51
isPrimitive ,
38
52
isStateTreeNode ,
39
53
isType ,
40
- IType ,
41
- IValidationResult ,
42
54
mobxShallow ,
43
55
optional ,
44
- MapType ,
45
- typecheckInternal ,
46
56
typeCheckFailure ,
47
- TypeFlags ,
48
- Hook ,
49
- AnyObjectNode ,
50
- AnyNode ,
51
- _CustomOrOther ,
52
- _NotCustomized ,
53
- Instance ,
54
- devMode ,
55
- assertIsString ,
56
- assertArg ,
57
- FunctionWithFlag ,
58
- type IStateTreeNode
57
+ typecheckInternal
59
58
} from "../../internal"
60
59
61
60
const PRE_PROCESS_SNAPSHOT = "preProcessSnapshot"
@@ -74,6 +73,9 @@ export interface ModelPropertiesDeclaration {
74
73
[ key : string ] : ModelPrimitive | IAnyType
75
74
}
76
75
76
+ /** intersect two object types, but omit keys of B from A before doing so */
77
+ type OmitMerge < A , B > = Omit < A , keyof B > & B
78
+
77
79
/**
78
80
* Unmaps syntax property declarations to a map of { propName: IType }
79
81
*
@@ -117,6 +119,8 @@ type IsOptionalValue<C, TV, FV> = undefined extends C ? TV : FV
117
119
// type _E = IsOptionalValue<any, true, false> // true
118
120
// type _F = IsOptionalValue<unknown, true, false> // true
119
121
122
+ type AnyObject = Record < string , any >
123
+
120
124
/**
121
125
* Name of the properties of an object that can't be set to undefined, any or unknown
122
126
* @hidden
@@ -199,23 +203,28 @@ export interface IModelType<
199
203
// so it is recommended to use pre/post process snapshot after all props have been defined
200
204
props < PROPS2 extends ModelPropertiesDeclaration > (
201
205
props : PROPS2
202
- ) : IModelType < PROPS & ModelPropertiesDeclarationToProperties < PROPS2 > , OTHERS , CustomC , CustomS >
206
+ ) : IModelType <
207
+ OmitMerge < PROPS , ModelPropertiesDeclarationToProperties < PROPS2 > > ,
208
+ OTHERS ,
209
+ CustomC ,
210
+ CustomS
211
+ >
203
212
204
- views < V extends Object > (
213
+ views < V extends AnyObject > (
205
214
fn : ( self : Instance < this> ) => V
206
- ) : IModelType < PROPS , OTHERS & V , CustomC , CustomS >
215
+ ) : IModelType < PROPS , OmitMerge < OTHERS , V > , CustomC , CustomS >
207
216
208
217
actions < A extends ModelActions > (
209
218
fn : ( self : Instance < this> ) => A
210
- ) : IModelType < PROPS , OTHERS & A , CustomC , CustomS >
219
+ ) : IModelType < PROPS , OmitMerge < OTHERS , A > , CustomC , CustomS >
211
220
212
- volatile < TP extends object > (
213
- fn : ( self : Instance < this> ) => TP
214
- ) : IModelType < PROPS , OTHERS & TP , CustomC , CustomS >
221
+ volatile < VS extends AnyObject > (
222
+ fn : ( self : Instance < this> ) => VS
223
+ ) : IModelType < PROPS , OmitMerge < OTHERS , VS > , CustomC , CustomS >
215
224
216
- extend < A extends ModelActions = { } , V extends Object = { } , VS extends Object = { } > (
225
+ extend < A extends ModelActions = { } , V extends AnyObject = { } , VS extends AnyObject = { } > (
217
226
fn : ( self : Instance < this> ) => { actions ?: A ; views ?: V ; state ?: VS }
218
- ) : IModelType < PROPS , OTHERS & A & V & VS , CustomC , CustomS >
227
+ ) : IModelType < PROPS , OmitMerge < OTHERS , A & V & VS > , CustomC , CustomS >
219
228
220
229
preProcessSnapshot < NewC = ModelCreationType2 < PROPS , CustomC > > (
221
230
fn : ( snapshot : NewC ) => WithAdditionalProperties < ModelCreationType2 < PROPS , CustomC > >
@@ -235,6 +244,7 @@ export interface IAnyModelType extends IModelType<any, any, any, any> {}
235
244
export type ExtractProps < T extends IAnyModelType > = T extends IModelType < infer P , any , any , any >
236
245
? P
237
246
: never
247
+
238
248
/** @hidden */
239
249
export type ExtractOthers < T extends IAnyModelType > = T extends IModelType < any , infer O , any , any >
240
250
? O
@@ -463,7 +473,7 @@ export class ModelType<
463
473
return this . cloneAndEnhance ( { properties } )
464
474
}
465
475
466
- volatile < TP extends object > ( fn : ( self : Instance < this> ) => TP ) {
476
+ volatile < TP extends AnyObject > ( fn : ( self : Instance < this> ) => TP ) {
467
477
if ( typeof fn !== "function" ) {
468
478
throw new MstError (
469
479
`You passed an ${ typeof fn } to volatile state as an argument, when function is expected`
@@ -496,7 +506,7 @@ export class ModelType<
496
506
} )
497
507
}
498
508
499
- extend < A extends ModelActions = { } , V extends Object = { } , VS extends Object = { } > (
509
+ extend < A extends ModelActions = { } , V extends AnyObject = { } , VS extends AnyObject = { } > (
500
510
fn : ( self : Instance < this> ) => { actions ?: A ; views ?: V ; state ?: VS }
501
511
) {
502
512
const initializer = ( self : Instance < this> ) => {
@@ -513,15 +523,15 @@ export class ModelType<
513
523
return this . cloneAndEnhance ( { initializers : [ initializer ] } )
514
524
}
515
525
516
- views < V extends Object > ( fn : ( self : Instance < this> ) => V ) {
526
+ views < V extends AnyObject > ( fn : ( self : Instance < this> ) => V ) {
517
527
const viewInitializer = ( self : Instance < this> ) => {
518
528
this . instantiateViews ( self , fn ( self ) )
519
529
return self
520
530
}
521
531
return this . cloneAndEnhance ( { initializers : [ viewInitializer ] } )
522
532
}
523
533
524
- private instantiateViews ( self : this[ "T" ] , views : Object ) : void {
534
+ private instantiateViews ( self : this[ "T" ] , views : AnyObject ) : void {
525
535
// check views return
526
536
if ( ! isPlainObject ( views ) ) {
527
537
throw new MstError ( `views initializer should return a plain object containing views` )
0 commit comments