mobx-keystone 1.20.0 → 1.22.0

This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
Files changed (174) hide show
  1. package/CHANGELOG.md +864 -853
  2. package/README.md +61 -61
  3. package/dist/mobx-keystone.esm.js +11336 -11148
  4. package/dist/mobx-keystone.esm.mjs +11336 -11148
  5. package/dist/mobx-keystone.umd.js +12229 -11738
  6. package/dist/types/actionMiddlewares/undoMiddleware.d.ts +1 -1
  7. package/dist/types/modelShared/prop.d.ts +5 -0
  8. package/dist/types/redux/connectReduxDevTools.d.ts +17 -4
  9. package/dist/types/ref/Ref.d.ts +1 -1
  10. package/dist/types/treeUtils/sandbox.d.ts +1 -1
  11. package/dist/types/types/TypeChecker.d.ts +1 -1
  12. package/dist/types/types/arrayBased/typesArray.d.ts +1 -1
  13. package/dist/types/types/arrayBased/typesTuple.d.ts +1 -1
  14. package/dist/types/types/objectBased/typesArraySet.d.ts +1 -1
  15. package/dist/types/types/objectBased/typesDataModelData.d.ts +1 -1
  16. package/dist/types/types/objectBased/typesModel.d.ts +1 -1
  17. package/dist/types/types/objectBased/typesObject.d.ts +3 -3
  18. package/dist/types/types/objectBased/typesObjectMap.d.ts +1 -1
  19. package/dist/types/types/objectBased/typesRecord.d.ts +1 -1
  20. package/dist/types/types/primitiveBased/typesPrimitive.d.ts +1 -1
  21. package/dist/types/types/types.d.ts +3 -3
  22. package/dist/types/types/utility/typesCodec.d.ts +1 -6
  23. package/dist/types/types/utility/typesCodecCore.d.ts +1 -1
  24. package/dist/types/types/utility/typesOr.d.ts +1 -1
  25. package/dist/types/types/utility/typesRefinement.d.ts +1 -1
  26. package/dist/types/types/utility/typesSkipCheck.d.ts +1 -1
  27. package/dist/types/types/utility/typesTag.d.ts +1 -1
  28. package/dist/types/wrappers/ObjectMap.d.ts +2 -0
  29. package/dist/types/wrappers/asMap.d.ts +10 -6
  30. package/package.json +14 -12
  31. package/src/action/applyAction.ts +125 -114
  32. package/src/action/applyMethodCall.ts +37 -37
  33. package/src/action/isModelAction.ts +16 -16
  34. package/src/action/middleware.ts +137 -137
  35. package/src/action/modelFlow.ts +240 -240
  36. package/src/action/modelFlowPromiseGenerator.ts +127 -125
  37. package/src/action/pendingActions.ts +42 -42
  38. package/src/action/wrapInAction.ts +156 -156
  39. package/src/actionMiddlewares/actionSerialization/actionSerialization.ts +1 -1
  40. package/src/actionMiddlewares/actionSerialization/applySerializedAction.ts +172 -172
  41. package/src/actionMiddlewares/actionSerialization/arraySerializer.ts +20 -20
  42. package/src/actionMiddlewares/actionSerialization/dateSerializer.ts +17 -17
  43. package/src/actionMiddlewares/actionSerialization/mapSerializer.ts +42 -42
  44. package/src/actionMiddlewares/actionSerialization/objectPathSerializer.ts +50 -50
  45. package/src/actionMiddlewares/actionSerialization/objectSnapshotSerializer.ts +21 -21
  46. package/src/actionMiddlewares/actionSerialization/plainObjectSerializer.ts +24 -32
  47. package/src/actionMiddlewares/actionSerialization/primitiveSerializer.ts +51 -51
  48. package/src/actionMiddlewares/actionSerialization/setSerializer.ts +37 -37
  49. package/src/actionMiddlewares/actionTrackingMiddleware.ts +3 -3
  50. package/src/actionMiddlewares/onActionMiddleware.ts +2 -2
  51. package/src/actionMiddlewares/readonlyMiddleware.ts +2 -2
  52. package/src/actionMiddlewares/transactionMiddleware.ts +4 -4
  53. package/src/actionMiddlewares/undoMiddleware.ts +804 -796
  54. package/src/actionMiddlewares/utils.ts +2 -2
  55. package/src/computedTree/computedTree.ts +143 -143
  56. package/src/context/context.ts +1 -1
  57. package/src/dataModel/BaseDataModel.ts +194 -192
  58. package/src/dataModel/actions.ts +1 -1
  59. package/src/dataModel/newDataModel.ts +57 -57
  60. package/src/deepChange/index.ts +1 -1
  61. package/src/deepChange/onDeepChange.ts +244 -244
  62. package/src/frozen/Frozen.ts +146 -146
  63. package/src/globalConfig/globalConfig.ts +107 -107
  64. package/src/index.ts +24 -24
  65. package/src/model/BaseModel.ts +275 -273
  66. package/src/model/ModelConstructorOptions.ts +15 -15
  67. package/src/model/getModelMetadata.ts +51 -51
  68. package/src/model/index.ts +6 -6
  69. package/src/model/mixins.ts +288 -288
  70. package/src/model/newModel.ts +301 -301
  71. package/src/model/utils.ts +249 -249
  72. package/src/modelShared/modelDecorator.ts +260 -253
  73. package/src/modelShared/modelInfo.ts +50 -50
  74. package/src/modelShared/modelSymbols.ts +3 -3
  75. package/src/modelShared/newModel.ts +25 -25
  76. package/src/modelShared/prop.ts +713 -684
  77. package/src/modelShared/sharedInternalModel.ts +427 -417
  78. package/src/modelShared/utils.ts +24 -24
  79. package/src/parent/core.ts +65 -65
  80. package/src/parent/coreObjectChildren.ts +180 -180
  81. package/src/parent/detach.ts +47 -47
  82. package/src/parent/findParent.ts +1 -1
  83. package/src/parent/getChildrenObjects.ts +25 -25
  84. package/src/parent/path.ts +386 -381
  85. package/src/parent/setParent.ts +149 -149
  86. package/src/parent/walkTree.ts +1 -1
  87. package/src/patch/applyPatches.ts +187 -187
  88. package/src/patch/jsonPatch.ts +108 -108
  89. package/src/patch/patchRecorder.ts +6 -1
  90. package/src/redux/connectReduxDevTools.ts +39 -7
  91. package/src/redux/redux.ts +6 -2
  92. package/src/ref/Ref.ts +1 -1
  93. package/src/ref/core.ts +339 -345
  94. package/src/ref/customRef.ts +3 -3
  95. package/src/ref/rootRef.ts +100 -100
  96. package/src/rootStore/attachDetach.ts +89 -89
  97. package/src/rootStore/rootStore.ts +1 -1
  98. package/src/snapshot/SnapshotOf.ts +104 -104
  99. package/src/snapshot/SnapshotProcessingError.ts +45 -45
  100. package/src/snapshot/applySnapshot.ts +177 -181
  101. package/src/snapshot/cloneTreeValue.ts +30 -30
  102. package/src/snapshot/fromArraySnapshot.ts +27 -33
  103. package/src/snapshot/fromModelSnapshot.ts +70 -70
  104. package/src/snapshot/fromPlainObjectSnapshot.ts +47 -42
  105. package/src/snapshot/fromSnapshot.ts +198 -198
  106. package/src/snapshot/getSnapshot.ts +80 -79
  107. package/src/snapshot/index.ts +8 -8
  108. package/src/snapshot/internal.ts +230 -230
  109. package/src/snapshot/reconcileArraySnapshot.ts +70 -70
  110. package/src/snapshot/reconcileModelSnapshot.ts +155 -150
  111. package/src/snapshot/reconcilePlainObjectSnapshot.ts +72 -72
  112. package/src/snapshot/reconcileSnapshot.ts +88 -88
  113. package/src/standardActions/actions.ts +64 -64
  114. package/src/standardActions/objectActions.ts +60 -60
  115. package/src/transforms/ImmutableDate.ts +93 -93
  116. package/src/transforms/asMap.ts +50 -50
  117. package/src/transforms/asSet.ts +22 -22
  118. package/src/transforms/bigint.ts +20 -20
  119. package/src/transforms/date.ts +40 -40
  120. package/src/treeUtils/deepEquals.ts +55 -55
  121. package/src/treeUtils/sandbox.ts +6 -3
  122. package/src/tweaker/TweakerPriority.ts +9 -9
  123. package/src/tweaker/tweak.ts +4 -3
  124. package/src/tweaker/tweakArray.ts +530 -475
  125. package/src/tweaker/tweakFrozen.ts +42 -42
  126. package/src/tweaker/tweakModel.ts +1 -1
  127. package/src/tweaker/tweakPlainObject.ts +355 -355
  128. package/src/tweaker/typeChecking.ts +101 -101
  129. package/src/types/SnapshotTypeMismatchError.ts +46 -46
  130. package/src/types/TypeCheckError.ts +183 -184
  131. package/src/types/TypeChecker.ts +202 -185
  132. package/src/types/arrayBased/typesArray.ts +136 -137
  133. package/src/types/arrayBased/typesTuple.ts +138 -139
  134. package/src/types/createPerEntryCachedCheck.ts +166 -160
  135. package/src/types/index.ts +6 -6
  136. package/src/types/objectBased/typesArraySet.ts +133 -134
  137. package/src/types/objectBased/typesDataModelData.ts +203 -204
  138. package/src/types/objectBased/typesModel.ts +220 -221
  139. package/src/types/objectBased/typesObject.ts +308 -310
  140. package/src/types/objectBased/typesObjectMap.ts +142 -143
  141. package/src/types/objectBased/typesRecord.ts +156 -157
  142. package/src/types/objectBased/typesRef.ts +89 -89
  143. package/src/types/primitiveBased/typesEnum.ts +63 -63
  144. package/src/types/primitiveBased/typesPrimitive.ts +219 -220
  145. package/src/types/registerDefaultStandardTypeResolvers.ts +21 -21
  146. package/src/types/resolveTypeChecker.ts +78 -78
  147. package/src/types/schemas.ts +208 -208
  148. package/src/types/tProp.ts +277 -268
  149. package/src/types/typeCheck.ts +21 -21
  150. package/src/types/typeCheckErrorUtils.ts +23 -23
  151. package/src/types/types.ts +142 -142
  152. package/src/types/utility/typeToStoredData.ts +37 -37
  153. package/src/types/utility/typesBigInt.ts +15 -15
  154. package/src/types/utility/typesCodec.ts +41 -38
  155. package/src/types/utility/typesCodecCore.ts +178 -178
  156. package/src/types/utility/typesCodecSupport.ts +806 -806
  157. package/src/types/utility/typesDate.ts +27 -27
  158. package/src/types/utility/typesMap.ts +253 -222
  159. package/src/types/utility/typesOr.ts +263 -259
  160. package/src/types/utility/typesRefinement.ts +131 -125
  161. package/src/types/utility/typesSet.ts +110 -110
  162. package/src/types/utility/typesSkipCheck.ts +69 -70
  163. package/src/types/utility/typesTag.ts +88 -82
  164. package/src/types/utility/typesUnchecked.ts +40 -40
  165. package/src/utils/AnyFunction.ts +4 -4
  166. package/src/utils/ModelPool.ts +59 -59
  167. package/src/utils/chainFns.ts +18 -18
  168. package/src/utils/decorators.ts +274 -274
  169. package/src/utils/errorDiagnostics.ts +204 -204
  170. package/src/utils/index.ts +443 -372
  171. package/src/wrappers/ArraySet.ts +156 -156
  172. package/src/wrappers/ObjectMap.ts +155 -136
  173. package/src/wrappers/asMap.ts +65 -30
  174. package/src/wrappers/asSet.ts +3 -3
@@ -1,268 +1,277 @@
1
- import { AnyModelProp, MaybeOptionalModelProp, OptionalModelProp, prop } from "../modelShared/prop"
2
- import { lazy } from "../utils"
3
- import {
4
- typesBoolean,
5
- typesNull,
6
- typesNumber,
7
- typesString,
8
- typesUndefined,
9
- } from "./primitiveBased/typesPrimitive"
10
- import { resolveStandardType, resolveTypeChecker } from "./resolveTypeChecker"
11
- import type { AnyStandardType, AnyType, TypeToData, TypeToSnapshotIn } from "./schemas"
12
- import { createCodecPropTransform, resolveCodecSupport } from "./utility/typesCodec"
13
- import { typesOr } from "./utility/typesOr"
14
- import type { TypeToStoredData } from "./utility/typeToStoredData"
15
-
16
- const noDefaultValueSymbol = Symbol("noDefaultValue")
17
-
18
- const tPropCache = new WeakMap<AnyStandardType, Map<unknown, AnyModelProp>>()
19
-
20
- type AnyTypeOrArray = AnyType | ReadonlyArray<AnyType>
21
-
22
- type StoredRequiredPropMetadata<TType extends AnyType> = {
23
- $storedValueType: TypeToStoredData<TType>
24
- $storedCreationValueType: TypeToStoredData<TType>
25
- }
26
-
27
- type TypedMaybeOptionalModelProp<TType extends AnyType> = MaybeOptionalModelProp<
28
- TypeToData<TType>
29
- > &
30
- StoredRequiredPropMetadata<TType>
31
-
32
- type TypedOptionalModelProp<TType extends AnyType> = OptionalModelProp<TypeToData<TType>> & {
33
- $storedValueType: TypeToStoredData<TType>
34
- $storedCreationValueType: TypeToStoredData<TType> | null | undefined
35
- $typedFromSnapshotOverride: TypeToSnapshotIn<TType> | null | undefined
36
- }
37
-
38
- function getOrCreateTProp(
39
- type: AnyStandardType,
40
- defKey: unknown,
41
- createTProp: () => AnyModelProp
42
- ): AnyModelProp {
43
- let defValueCache = tPropCache.get(type)
44
- if (!defValueCache) {
45
- defValueCache = new Map()
46
- tPropCache.set(type, defValueCache)
47
- }
48
-
49
- let prop = defValueCache.get(defKey)
50
- if (!prop) {
51
- prop = createTProp()
52
- defValueCache.set(defKey, prop)
53
- }
54
-
55
- return prop
56
- }
57
-
58
- /**
59
- * Defines a string model property with a default value.
60
- * Equivalent to `tProp(types.string, defaultValue)`.
61
- *
62
- * Example:
63
- * ```ts
64
- * x: tProp("foo") // an optional string that will take the value `"foo"` when undefined.
65
- * ```
66
- *
67
- * @param defaultValue Default value.
68
- * @returns
69
- */
70
- export function tProp(defaultValue: string): TypedOptionalModelProp<typeof typesString>
71
-
72
- /**
73
- * Defines a number model property with a default value.
74
- * Equivalent to `tProp(types.number, defaultValue)`.
75
- *
76
- * Example:
77
- * ```ts
78
- * x: tProp(42) // an optional number that will take the value `42` when undefined.
79
- * ```
80
- *
81
- * @param defaultValue Default value.
82
- * @returns
83
- */
84
- export function tProp(defaultValue: number): TypedOptionalModelProp<typeof typesNumber>
85
-
86
- /**
87
- * Defines a boolean model property with a default value.
88
- * Equivalent to `tProp(types.boolean, defaultValue)`.
89
- *
90
- * Example:
91
- * ```ts
92
- * x: tProp(true) // an optional boolean that will take the value `true` when undefined.
93
- * ```
94
- *
95
- * @param defaultValue Default value.
96
- * @returns
97
- */
98
- export function tProp(defaultValue: boolean): TypedOptionalModelProp<typeof typesBoolean>
99
-
100
- /**
101
- * Defines a model property, with an optional function to generate a default value
102
- * if the input snapshot / model creation data is `null` or `undefined` and with an associated type checker.
103
- *
104
- * Example:
105
- * ```ts
106
- * x: tProp(types.number, () => 10) // an optional number, with a default value of 10
107
- * x: tProp(types.array(types.number), () => []) // an optional number array, with a default empty array
108
- * ```
109
- *
110
- * @template TType Type checker type.
111
- *
112
- * @param type Type checker.
113
- * @param defaultFn Default value generator function.
114
- * @returns
115
- */
116
- export function tProp<TType extends AnyType>(
117
- type: TType,
118
- defaultFn: () => TypeToData<TType>
119
- ): TypedOptionalModelProp<TType>
120
-
121
- /**
122
- * Defines a model property using array syntax as a shorthand for `types.or(...)`,
123
- * with an optional function to generate a default value if the input snapshot /
124
- * model creation data is `null` or `undefined`.
125
- *
126
- * Example:
127
- * ```ts
128
- * x: tProp([String, Number], () => 10) // equivalent to tProp(types.or(String, Number), () => 10)
129
- * ```
130
- *
131
- * @template TType Array of possible type checkers.
132
- *
133
- * @param type Array shorthand for `types.or(...)`.
134
- * @param defaultFn Default value generator function.
135
- * @returns
136
- */
137
- export function tProp<TType extends ReadonlyArray<AnyType>>(
138
- type: TType,
139
- defaultFn: () => TypeToData<TType[number]>
140
- ): TypedOptionalModelProp<TType[number]>
141
-
142
- /**
143
- * Defines a model property, with an optional default value
144
- * if the input snapshot / model creation data is `null` or `undefined` and with an associated type checker.
145
- * You should only use this with primitive values and never with object values
146
- * (array, model, object, etc).
147
- *
148
- * Example:
149
- * ```ts
150
- * x: tProp(types.number, 10) // an optional number, with a default value of 10
151
- * ```
152
- *
153
- * @template TType Type checker type.
154
- *
155
- * @param type Type checker.
156
- * @param defaultValue Default value generator function.
157
- * @returns
158
- */
159
- export function tProp<TType extends AnyType>(
160
- type: TType,
161
- defaultValue: TypeToData<TType>
162
- ): TypedOptionalModelProp<TType>
163
-
164
- /**
165
- * Defines a model property using array syntax as a shorthand for `types.or(...)`,
166
- * with an optional default value if the input snapshot / model creation data is
167
- * `null` or `undefined`.
168
- *
169
- * Example:
170
- * ```ts
171
- * x: tProp([String, Number], "foo") // equivalent to tProp(types.or(String, Number), "foo")
172
- * ```
173
- *
174
- * @template TType Array of possible type checkers.
175
- *
176
- * @param type Array shorthand for `types.or(...)`.
177
- * @param defaultValue Default value.
178
- * @returns
179
- */
180
- export function tProp<TType extends ReadonlyArray<AnyType>>(
181
- type: TType,
182
- defaultValue: TypeToData<TType[number]>
183
- ): TypedOptionalModelProp<TType[number]>
184
-
185
- /**
186
- * Defines a model property with no default value and an associated type checker.
187
- *
188
- * Example:
189
- * ```ts
190
- * x: tProp(types.number) // a required number
191
- * x: tProp(types.maybe(types.number)) // an optional number, which defaults to undefined
192
- * ```
193
- *
194
- * @template TType Type checker type.
195
- *
196
- * @param type Type checker.
197
- * @returns
198
- */
199
- export function tProp<TType extends AnyType>(type: TType): TypedMaybeOptionalModelProp<TType>
200
-
201
- /**
202
- * Defines a model property with no default value using array syntax as a shorthand
203
- * for `types.or(...)`.
204
- *
205
- * Example:
206
- * ```ts
207
- * x: tProp([String, undefined]) // equivalent to tProp(types.or(String, undefined))
208
- * ```
209
- *
210
- * @template TType Array of possible type checkers.
211
- *
212
- * @param type Array shorthand for `types.or(...)`.
213
- * @returns
214
- */
215
- export function tProp<TType extends ReadonlyArray<AnyType>>(
216
- type: TType
217
- ): TypedMaybeOptionalModelProp<TType[number]>
218
-
219
- export function tProp(typeOrDefaultValue: any, def?: any): AnyModelProp {
220
- switch (typeof typeOrDefaultValue) {
221
- case "string":
222
- return tProp(typesString, typeOrDefaultValue)
223
- case "number":
224
- return tProp(typesNumber, typeOrDefaultValue)
225
- case "boolean":
226
- return tProp(typesBoolean, typeOrDefaultValue)
227
- default:
228
- break
229
- }
230
-
231
- const hasDefaultValue = arguments.length >= 2
232
-
233
- const typeOrArray: AnyTypeOrArray = typeOrDefaultValue
234
- const resolvedType: AnyType = Array.isArray(typeOrArray)
235
- ? typesOr(...(typeOrArray as ReadonlyArray<AnyType>))
236
- : (typeOrArray as AnyType)
237
-
238
- const typeChecker = resolveStandardType(resolvedType)
239
- const codecSupport = resolveCodecSupport(typeChecker)
240
-
241
- return getOrCreateTProp(typeChecker, hasDefaultValue ? def : noDefaultValueSymbol, () => {
242
- const fromSnapshotTypeChecker = hasDefaultValue
243
- ? typesOr(codecSupport.storedType, typesUndefined, typesNull)
244
- : codecSupport.storedType
245
- const getFromSnapshotProcessor = lazy(
246
- () => resolveTypeChecker(fromSnapshotTypeChecker).fromSnapshotProcessor
247
- )
248
- const getToSnapshotProcessor = lazy(
249
- () => resolveTypeChecker(codecSupport.storedType).toSnapshotProcessor
250
- )
251
-
252
- // we use Object.create to avoid messing up with the prop cache
253
- const baseProp = hasDefaultValue ? prop(def) : prop()
254
- const newProp = codecSupport.hasCodec
255
- ? baseProp.withTransform(createCodecPropTransform(typeChecker))
256
- : Object.create(baseProp)
257
-
258
- Object.assign(newProp, {
259
- _typeChecker: typeChecker,
260
-
261
- _fromSnapshotProcessor: (sn) => getFromSnapshotProcessor()(sn),
262
-
263
- _toSnapshotProcessor: (sn) => getToSnapshotProcessor()(sn),
264
- } satisfies Partial<AnyModelProp>)
265
-
266
- return newProp
267
- })
268
- }
1
+ import {
2
+ type AnyModelProp,
3
+ type MaybeOptionalModelProp,
4
+ type OptionalModelProp,
5
+ prop,
6
+ } from "../modelShared/prop"
7
+ import { lazy } from "../utils"
8
+ import {
9
+ typesBoolean,
10
+ typesNull,
11
+ typesNumber,
12
+ typesString,
13
+ typesUndefined,
14
+ } from "./primitiveBased/typesPrimitive"
15
+ import { resolveStandardType, resolveTypeChecker } from "./resolveTypeChecker"
16
+ import type { AnyStandardType, AnyType, TypeToData, TypeToSnapshotIn } from "./schemas"
17
+ import { createCodecPropTransform, resolveCodecSupport } from "./utility/typesCodec"
18
+ import { typesOr } from "./utility/typesOr"
19
+ import type { TypeToStoredData } from "./utility/typeToStoredData"
20
+
21
+ const noDefaultValueSymbol = Symbol("noDefaultValue")
22
+
23
+ const tPropCache = new WeakMap<AnyStandardType, Map<unknown, AnyModelProp>>()
24
+
25
+ type AnyTypeOrArray = AnyType | ReadonlyArray<AnyType>
26
+
27
+ type StoredRequiredPropMetadata<TType extends AnyType> = {
28
+ $storedValueType: TypeToStoredData<TType>
29
+ $storedCreationValueType: TypeToStoredData<TType>
30
+ }
31
+
32
+ type TypedMaybeOptionalModelProp<TType extends AnyType> = MaybeOptionalModelProp<
33
+ TypeToData<TType>
34
+ > &
35
+ StoredRequiredPropMetadata<TType>
36
+
37
+ type TypedOptionalModelProp<TType extends AnyType> = OptionalModelProp<TypeToData<TType>> & {
38
+ $storedValueType: TypeToStoredData<TType>
39
+ $storedCreationValueType: TypeToStoredData<TType> | null | undefined
40
+ $typedFromSnapshotOverride: TypeToSnapshotIn<TType> | null | undefined
41
+ }
42
+
43
+ function getOrCreateTProp(
44
+ type: AnyStandardType,
45
+ defKey: unknown,
46
+ createTProp: () => AnyModelProp
47
+ ): AnyModelProp {
48
+ let defValueCache = tPropCache.get(type)
49
+ if (!defValueCache) {
50
+ defValueCache = new Map()
51
+ tPropCache.set(type, defValueCache)
52
+ }
53
+
54
+ let prop = defValueCache.get(defKey)
55
+ if (!prop) {
56
+ prop = createTProp()
57
+ defValueCache.set(defKey, prop)
58
+ }
59
+
60
+ return prop
61
+ }
62
+
63
+ /**
64
+ * Defines a string model property with a default value.
65
+ * Equivalent to `tProp(types.string, defaultValue)`.
66
+ *
67
+ * Example:
68
+ * ```ts
69
+ * x: tProp("foo") // an optional string that will take the value `"foo"` when undefined.
70
+ * ```
71
+ *
72
+ * @param defaultValue Default value.
73
+ * @returns
74
+ */
75
+ export function tProp(defaultValue: string): TypedOptionalModelProp<typeof typesString>
76
+
77
+ /**
78
+ * Defines a number model property with a default value.
79
+ * Equivalent to `tProp(types.number, defaultValue)`.
80
+ *
81
+ * Example:
82
+ * ```ts
83
+ * x: tProp(42) // an optional number that will take the value `42` when undefined.
84
+ * ```
85
+ *
86
+ * @param defaultValue Default value.
87
+ * @returns
88
+ */
89
+ export function tProp(defaultValue: number): TypedOptionalModelProp<typeof typesNumber>
90
+
91
+ /**
92
+ * Defines a boolean model property with a default value.
93
+ * Equivalent to `tProp(types.boolean, defaultValue)`.
94
+ *
95
+ * Example:
96
+ * ```ts
97
+ * x: tProp(true) // an optional boolean that will take the value `true` when undefined.
98
+ * ```
99
+ *
100
+ * @param defaultValue Default value.
101
+ * @returns
102
+ */
103
+ export function tProp(defaultValue: boolean): TypedOptionalModelProp<typeof typesBoolean>
104
+
105
+ /**
106
+ * Defines a model property, with an optional function to generate a default value
107
+ * if the input snapshot / model creation data is `null` or `undefined` and with an associated type checker.
108
+ *
109
+ * Example:
110
+ * ```ts
111
+ * x: tProp(types.number, () => 10) // an optional number, with a default value of 10
112
+ * x: tProp(types.array(types.number), () => []) // an optional number array, with a default empty array
113
+ * ```
114
+ *
115
+ * @template TType Type checker type.
116
+ *
117
+ * @param type Type checker.
118
+ * @param defaultFn Default value generator function.
119
+ * @returns
120
+ */
121
+ export function tProp<TType extends AnyType>(
122
+ type: TType,
123
+ defaultFn: () => TypeToData<TType>
124
+ ): TypedOptionalModelProp<TType>
125
+
126
+ /**
127
+ * Defines a model property using array syntax as a shorthand for `types.or(...)`,
128
+ * with an optional function to generate a default value if the input snapshot /
129
+ * model creation data is `null` or `undefined`.
130
+ *
131
+ * Example:
132
+ * ```ts
133
+ * x: tProp([String, Number], () => 10) // equivalent to tProp(types.or(String, Number), () => 10)
134
+ * ```
135
+ *
136
+ * @template TType Array of possible type checkers.
137
+ *
138
+ * @param type Array shorthand for `types.or(...)`.
139
+ * @param defaultFn Default value generator function.
140
+ * @returns
141
+ */
142
+ export function tProp<TType extends ReadonlyArray<AnyType>>(
143
+ type: TType,
144
+ defaultFn: () => TypeToData<TType[number]>
145
+ ): TypedOptionalModelProp<TType[number]>
146
+
147
+ /**
148
+ * Defines a model property, with an optional default value
149
+ * if the input snapshot / model creation data is `null` or `undefined` and with an associated type checker.
150
+ * You should only use this with primitive values and never with object values
151
+ * (array, model, object, etc).
152
+ *
153
+ * Example:
154
+ * ```ts
155
+ * x: tProp(types.number, 10) // an optional number, with a default value of 10
156
+ * ```
157
+ *
158
+ * @template TType Type checker type.
159
+ *
160
+ * @param type Type checker.
161
+ * @param defaultValue Default value generator function.
162
+ * @returns
163
+ */
164
+ export function tProp<TType extends AnyType>(
165
+ type: TType,
166
+ defaultValue: TypeToData<TType>
167
+ ): TypedOptionalModelProp<TType>
168
+
169
+ /**
170
+ * Defines a model property using array syntax as a shorthand for `types.or(...)`,
171
+ * with an optional default value if the input snapshot / model creation data is
172
+ * `null` or `undefined`.
173
+ *
174
+ * Example:
175
+ * ```ts
176
+ * x: tProp([String, Number], "foo") // equivalent to tProp(types.or(String, Number), "foo")
177
+ * ```
178
+ *
179
+ * @template TType Array of possible type checkers.
180
+ *
181
+ * @param type Array shorthand for `types.or(...)`.
182
+ * @param defaultValue Default value.
183
+ * @returns
184
+ */
185
+ export function tProp<TType extends ReadonlyArray<AnyType>>(
186
+ type: TType,
187
+ defaultValue: TypeToData<TType[number]>
188
+ ): TypedOptionalModelProp<TType[number]>
189
+
190
+ /**
191
+ * Defines a model property with no default value and an associated type checker.
192
+ *
193
+ * Example:
194
+ * ```ts
195
+ * x: tProp(types.number) // a required number
196
+ * x: tProp(types.maybe(types.number)) // an optional number, which defaults to undefined
197
+ * ```
198
+ *
199
+ * @template TType Type checker type.
200
+ *
201
+ * @param type Type checker.
202
+ * @returns
203
+ */
204
+ export function tProp<TType extends AnyType>(type: TType): TypedMaybeOptionalModelProp<TType>
205
+
206
+ /**
207
+ * Defines a model property with no default value using array syntax as a shorthand
208
+ * for `types.or(...)`.
209
+ *
210
+ * Example:
211
+ * ```ts
212
+ * x: tProp([String, undefined]) // equivalent to tProp(types.or(String, undefined))
213
+ * ```
214
+ *
215
+ * @template TType Array of possible type checkers.
216
+ *
217
+ * @param type Array shorthand for `types.or(...)`.
218
+ * @returns
219
+ */
220
+ export function tProp<TType extends ReadonlyArray<AnyType>>(
221
+ type: TType
222
+ ): TypedMaybeOptionalModelProp<TType[number]>
223
+
224
+ export function tProp(
225
+ ...args: [typeOrDefaultValue: any] | [typeOrDefaultValue: any, def: any]
226
+ ): AnyModelProp {
227
+ const [typeOrDefaultValue, def] = args
228
+ switch (typeof typeOrDefaultValue) {
229
+ case "string":
230
+ return tProp(typesString, typeOrDefaultValue)
231
+ case "number":
232
+ return tProp(typesNumber, typeOrDefaultValue)
233
+ case "boolean":
234
+ return tProp(typesBoolean, typeOrDefaultValue)
235
+ default:
236
+ break
237
+ }
238
+
239
+ const hasDefaultValue = args.length >= 2
240
+
241
+ const typeOrArray: AnyTypeOrArray = typeOrDefaultValue
242
+ const resolvedType: AnyType = Array.isArray(typeOrArray)
243
+ ? typesOr(...(typeOrArray as ReadonlyArray<AnyType>))
244
+ : (typeOrArray as AnyType)
245
+
246
+ const typeChecker = resolveStandardType(resolvedType)
247
+ const codecSupport = resolveCodecSupport(typeChecker)
248
+
249
+ return getOrCreateTProp(typeChecker, hasDefaultValue ? def : noDefaultValueSymbol, () => {
250
+ const fromSnapshotTypeChecker = hasDefaultValue
251
+ ? typesOr(codecSupport.storedType, typesUndefined, typesNull)
252
+ : codecSupport.storedType
253
+ const getFromSnapshotProcessor = lazy(
254
+ () => resolveTypeChecker(fromSnapshotTypeChecker).fromSnapshotProcessor
255
+ )
256
+ const getToSnapshotProcessor = lazy(
257
+ () => resolveTypeChecker(codecSupport.storedType).toSnapshotProcessor
258
+ )
259
+
260
+ // we use Object.create to avoid messing up with the prop cache
261
+ const baseProp = hasDefaultValue ? prop(def) : prop()
262
+ const newProp = codecSupport.hasCodec
263
+ ? baseProp.withTransform(createCodecPropTransform(typeChecker))
264
+ : Object.create(baseProp)
265
+
266
+ Object.assign(newProp, {
267
+ _defaultValueIsTransformed: codecSupport.hasCodec,
268
+ _typeChecker: typeChecker,
269
+
270
+ _fromSnapshotProcessor: (sn) => getFromSnapshotProcessor()(sn),
271
+
272
+ _toSnapshotProcessor: (sn) => getToSnapshotProcessor()(sn),
273
+ } satisfies Partial<AnyModelProp>)
274
+
275
+ return newProp
276
+ })
277
+ }
@@ -1,21 +1,21 @@
1
- import { resolveTypeChecker } from "./resolveTypeChecker"
2
- import type { AnyType, TypeToData } from "./schemas"
3
- import type { TypeCheckError } from "./TypeCheckError"
4
-
5
- /**
6
- * Checks if a value conforms to a given type.
7
- *
8
- * @template T Type.
9
- * @param type Type to check for.
10
- * @param value Value to check.
11
- * @returns A TypeError if the check fails or null if no error.
12
- */
13
- export function typeCheck<T extends AnyType>(type: T, value: TypeToData<T>): TypeCheckError | null {
14
- const typeChecker = resolveTypeChecker(type)
15
-
16
- if (typeChecker.unchecked || typeChecker.skipCheck) {
17
- return null
18
- }
19
-
20
- return typeChecker.check(value, [], value)
21
- }
1
+ import { resolveTypeChecker } from "./resolveTypeChecker"
2
+ import type { AnyType, TypeToData } from "./schemas"
3
+ import type { TypeCheckError } from "./TypeCheckError"
4
+
5
+ /**
6
+ * Checks if a value conforms to a given type.
7
+ *
8
+ * @template T Type.
9
+ * @param type Type to check for.
10
+ * @param value Value to check.
11
+ * @returns A TypeError if the check fails or null if no error.
12
+ */
13
+ export function typeCheck<T extends AnyType>(type: T, value: TypeToData<T>): TypeCheckError | null {
14
+ const typeChecker = resolveTypeChecker(type)
15
+
16
+ if (typeChecker.unchecked || typeChecker.skipCheck) {
17
+ return null
18
+ }
19
+
20
+ return typeChecker.check(value, [], value)
21
+ }
@@ -1,23 +1,23 @@
1
- import type { Path, PathElement } from "../parent/pathTypes"
2
- import { TypeCheckError } from "./TypeCheckError"
3
-
4
- /**
5
- * Prefixes an existing type-check error path with one parent path segment.
6
- * @internal
7
- */
8
- export function prependPathElementToTypeCheckError(
9
- valueError: TypeCheckError,
10
- path: Path,
11
- pathElement: PathElement,
12
- typeCheckedValue: any
13
- ): TypeCheckError {
14
- const fullPath =
15
- path.length > 0 ? [...path, pathElement, ...valueError.path] : [pathElement, ...valueError.path]
16
-
17
- return new TypeCheckError({
18
- path: fullPath,
19
- expectedTypeName: valueError.expectedTypeName,
20
- actualValue: valueError.actualValue,
21
- typeCheckedValue,
22
- })
23
- }
1
+ import type { Path, PathElement } from "../parent/pathTypes"
2
+ import { TypeCheckError } from "./TypeCheckError"
3
+
4
+ /**
5
+ * Prefixes an existing type-check error path with one parent path segment.
6
+ * @internal
7
+ */
8
+ export function prependPathElementToTypeCheckError(
9
+ valueError: TypeCheckError,
10
+ path: Path,
11
+ pathElement: PathElement,
12
+ typeCheckedValue: any
13
+ ): TypeCheckError {
14
+ const fullPath =
15
+ path.length > 0 ? [...path, pathElement, ...valueError.path] : [pathElement, ...valueError.path]
16
+
17
+ return new TypeCheckError({
18
+ path: fullPath,
19
+ expectedTypeName: valueError.expectedTypeName,
20
+ actualValue: valueError.actualValue,
21
+ typeCheckedValue,
22
+ })
23
+ }