@@ -81,20 +81,20 @@ export interface ModelPropertiesDeclaration {
8181 */
8282export type ModelPropertiesDeclarationToProperties < T extends ModelPropertiesDeclaration > =
8383 T extends { [ k : string ] : IAnyType } // optimization to reduce nesting
84- ? T
85- : {
86- [ K in keyof T ] : T [ K ] extends IAnyType // keep IAnyType check on the top to reduce nesting
87- ? T [ K ]
88- : T [ K ] extends string
89- ? IType < string | undefined , string , string >
90- : T [ K ] extends number
91- ? IType < number | undefined , number , number >
92- : T [ K ] extends boolean
93- ? IType < boolean | undefined , boolean , boolean >
94- : T [ K ] extends Date
95- ? IType < number | Date | undefined , number , Date >
96- : never
97- }
84+ ? T
85+ : {
86+ [ K in keyof T ] : T [ K ] extends IAnyType // keep IAnyType check on the top to reduce nesting
87+ ? T [ K ]
88+ : T [ K ] extends string
89+ ? IType < string | undefined , string , string >
90+ : T [ K ] extends number
91+ ? IType < number | undefined , number , number >
92+ : T [ K ] extends boolean
93+ ? IType < boolean | undefined , boolean , boolean >
94+ : T [ K ] extends Date
95+ ? IType < number | Date | undefined , number , Date >
96+ : never
97+ }
9898
9999/**
100100 * Checks if a value is optional (undefined, any or unknown).
@@ -124,7 +124,9 @@ type IsOptionalValue<C, TV, FV> = undefined extends C ? TV : FV
124124type DefinablePropsNames < T > = { [ K in keyof T ] : IsOptionalValue < T [ K ] , never , K > } [ keyof T ]
125125
126126/** @hidden */
127- export type ExtractCFromProps < P extends ModelProperties > = MaybeEmpty < { [ k in keyof P ] : P [ k ] [ "CreationType" ] } >
127+ export type ExtractCFromProps < P extends ModelProperties > = MaybeEmpty < {
128+ [ k in keyof P ] : P [ k ] [ "CreationType" ]
129+ } >
128130
129131/** @hidden */
130132export type MaybeEmpty < T > = keyof T extends never ? EmptyObject : T
@@ -147,7 +149,7 @@ export type ModelCreationType2<P extends ModelProperties, CustomC> = MaybeEmpty<
147149 keyof P extends never
148150 ? _CustomOrOther < CustomC , ModelCreationType < EmptyObject > >
149151 : _CustomOrOther < CustomC , ModelCreationType < ExtractCFromProps < P > > >
150- > ;
152+ >
151153
152154/** @hidden */
153155export type ModelSnapshotType < P extends ModelProperties > = {
@@ -227,7 +229,7 @@ export interface IModelType<
227229/**
228230 * Any model type.
229231 */
230- export interface IAnyModelType extends IModelType < any , any , any , any > { }
232+ export interface IAnyModelType extends IModelType < any , any , any , any > { }
231233
232234/** @hidden */
233235export type ExtractProps < T extends IAnyModelType > = T extends IModelType < infer P , any , any , any >
@@ -274,78 +276,82 @@ function toPropertiesObject(declaredProps: ModelPropertiesDeclaration): ModelPro
274276 } )
275277
276278 // loop through properties and ensures that all items are types
277- return keysList . reduce ( ( props , key ) => {
278- // warn if user intended a HOOK
279- if ( key in Hook ) {
280- throw new MstError (
281- `Hook '${ key } ' was defined as property. Hooks should be defined as part of the actions`
282- )
283- }
279+ return keysList . reduce (
280+ ( props , key ) => {
281+ // warn if user intended a HOOK
282+ if ( key in Hook ) {
283+ throw new MstError (
284+ `Hook '${ key } ' was defined as property. Hooks should be defined as part of the actions`
285+ )
286+ }
284287
285- // the user intended to use a view
286- const descriptor = Object . getOwnPropertyDescriptor ( declaredProps , key ) !
287- if ( "get" in descriptor ) {
288- throw new MstError ( "Getters are not supported as properties. Please use views instead" )
289- }
290- // undefined and null are not valid
291- const value = descriptor . value
292- if ( value === null || value === undefined ) {
293- throw new MstError (
294- "The default value of an attribute cannot be null or undefined as the type cannot be inferred. Did you mean `types.maybe(someType)`?"
295- )
296- }
297- // its a primitive, convert to its type
298- else if ( isPrimitive ( value ) ) {
299- props [ key ] = optional ( getPrimitiveFactoryFromValue ( value ) , value )
300- }
301- // map defaults to empty object automatically for models
302- else if ( value instanceof MapType ) {
303- props [ key ] = optional ( value , { } )
304- } else if ( value instanceof ArrayType ) {
305- props [ key ] = optional ( value , [ ] )
306- }
307- // its already a type
308- else if ( isType ( value ) ) {
309- // do nothing, it's already a type
310- }
311- // its a function, maybe the user wanted a view?
312- else if ( devMode ( ) && typeof value === "function" ) {
313- throw new MstError (
314- `Invalid type definition for property '${ key } ', it looks like you passed a function. Did you forget to invoke it, or did you intend to declare a view / action?`
315- )
316- }
317- // no other complex values
318- else if ( devMode ( ) && typeof value === "object" ) {
319- throw new MstError (
320- `Invalid type definition for property '${ key } ', it looks like you passed an object. Try passing another model type or a types.frozen.`
321- )
322- } else {
323- throw new MstError (
324- `Invalid type definition for property '${ key } ', cannot infer a type from a value like '${ value } ' (${ typeof value } )`
325- )
326- }
288+ // the user intended to use a view
289+ const descriptor = Object . getOwnPropertyDescriptor ( declaredProps , key ) !
290+ if ( "get" in descriptor ) {
291+ throw new MstError ( "Getters are not supported as properties. Please use views instead" )
292+ }
293+ // undefined and null are not valid
294+ const value = descriptor . value
295+ if ( value === null || value === undefined ) {
296+ throw new MstError (
297+ "The default value of an attribute cannot be null or undefined as the type cannot be inferred. Did you mean `types.maybe(someType)`?"
298+ )
299+ }
300+ // its a primitive, convert to its type
301+ else if ( isPrimitive ( value ) ) {
302+ props [ key ] = optional ( getPrimitiveFactoryFromValue ( value ) , value )
303+ }
304+ // map defaults to empty object automatically for models
305+ else if ( value instanceof MapType ) {
306+ props [ key ] = optional ( value , { } )
307+ } else if ( value instanceof ArrayType ) {
308+ props [ key ] = optional ( value , [ ] )
309+ }
310+ // its already a type
311+ else if ( isType ( value ) ) {
312+ // do nothing, it's already a type
313+ }
314+ // its a function, maybe the user wanted a view?
315+ else if ( devMode ( ) && typeof value === "function" ) {
316+ throw new MstError (
317+ `Invalid type definition for property '${ key } ', it looks like you passed a function. Did you forget to invoke it, or did you intend to declare a view / action?`
318+ )
319+ }
320+ // no other complex values
321+ else if ( devMode ( ) && typeof value === "object" ) {
322+ throw new MstError (
323+ `Invalid type definition for property '${ key } ', it looks like you passed an object. Try passing another model type or a types.frozen.`
324+ )
325+ } else {
326+ throw new MstError (
327+ `Invalid type definition for property '${ key } ', cannot infer a type from a value like '${ value } ' (${ typeof value } )`
328+ )
329+ }
327330
328- return props
329- } , { ...declaredProps } as any )
331+ return props
332+ } ,
333+ { ...declaredProps } as any
334+ )
330335}
331336
332337/**
333338 * @internal
334339 * @hidden
335340 */
336341export class ModelType <
337- PROPS extends ModelProperties ,
338- OTHERS ,
339- CustomC ,
340- CustomS ,
341- MT extends IModelType < PROPS , OTHERS , CustomC , CustomS >
342- >
342+ PROPS extends ModelProperties ,
343+ OTHERS ,
344+ CustomC ,
345+ CustomS ,
346+ MT extends IModelType < PROPS , OTHERS , CustomC , CustomS >
347+ >
343348 extends ComplexType <
344349 ModelCreationType2 < PROPS , CustomC > ,
345350 ModelSnapshotType2 < PROPS , CustomS > ,
346351 ModelInstanceType < PROPS , OTHERS >
347352 >
348- implements IModelType < PROPS , OTHERS , CustomC , CustomS > {
353+ implements IModelType < PROPS , OTHERS , CustomC , CustomS >
354+ {
349355 readonly flags = TypeFlags . Object
350356
351357 /*
@@ -439,8 +445,8 @@ export class ModelType<
439445 const actionInvoker = createActionInvoker ( self as any , name , boundAction )
440446 actions [ name ] = actionInvoker
441447
442- // See #646, allow models to be mocked
443- ; ( ! devMode ( ) ? addHiddenFinalProp : addHiddenWritableProp ) ( self , name , actionInvoker )
448+ // See #646, allow models to be mocked
449+ ; ( ! devMode ( ) ? addHiddenFinalProp : addHiddenWritableProp ) ( self , name , actionInvoker )
444450 } )
445451 }
446452
@@ -515,7 +521,7 @@ export class ModelType<
515521 } else if ( typeof descriptor . value === "function" ) {
516522 // this is a view function, merge as is!
517523 // See #646, allow models to be mocked
518- ; ( ! devMode ( ) ? addHiddenFinalProp : addHiddenWritableProp ) ( self , key , descriptor . value )
524+ ; ( ! devMode ( ) ? addHiddenFinalProp : addHiddenWritableProp ) ( self , key , descriptor . value )
519525 } else {
520526 throw new MstError ( `A view member should either be a function or getter based property` )
521527 }
@@ -645,7 +651,7 @@ export class ModelType<
645651 try {
646652 // TODO: FIXME, make sure the observable ref is used!
647653 const atom = getAtom ( node . storedValue , name )
648- ; ( atom as any ) . reportObserved ( )
654+ ; ( atom as any ) . reportObserved ( )
649655 } catch ( e ) {
650656 throw new MstError ( `${ name } property is declared twice` )
651657 }
@@ -669,14 +675,14 @@ export class ModelType<
669675 if ( ! ( patch . op === "replace" || patch . op === "add" ) ) {
670676 throw new MstError ( `object does not support operation ${ patch . op } ` )
671677 }
672- ; ( node . storedValue as any ) [ subpath ] = patch . value
678+ ; ( node . storedValue as any ) [ subpath ] = patch . value
673679 }
674680
675681 applySnapshot ( node : this[ "N" ] , snapshot : this[ "C" ] ) : void {
676682 typecheckInternal ( this , snapshot )
677683 const preProcessedSnapshot = this . applySnapshotPreProcessor ( snapshot )
678684 this . forAllProps ( ( name ) => {
679- ; ( node . storedValue as any ) [ name ] = preProcessedSnapshot [ name ]
685+ ; ( node . storedValue as any ) [ name ] = preProcessedSnapshot [ name ]
680686 } )
681687 }
682688
@@ -732,7 +738,7 @@ export class ModelType<
732738 }
733739
734740 removeChild ( node : this[ "N" ] , subpath : string ) {
735- ; ( node . storedValue as any ) [ subpath ] = undefined
741+ ; ( node . storedValue as any ) [ subpath ] = undefined
736742 }
737743}
738744ModelType . prototype . applySnapshot = action ( ModelType . prototype . applySnapshot )
@@ -853,6 +859,6 @@ export function compose(...args: any[]): any {
853859 * @param type
854860 * @returns
855861 */
856- export function isModelType < IT extends IAnyModelType = IAnyModelType > ( type : IAnyType ) : type is IT {
862+ export function isModelType ( type : unknown ) : type is IAnyModelType {
857863 return isType ( type ) && ( type . flags & TypeFlags . Object ) > 0
858864}
0 commit comments