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.
- package/CHANGELOG.md +864 -853
- package/README.md +61 -61
- package/dist/mobx-keystone.esm.js +11336 -11148
- package/dist/mobx-keystone.esm.mjs +11336 -11148
- package/dist/mobx-keystone.umd.js +12229 -11738
- package/dist/types/actionMiddlewares/undoMiddleware.d.ts +1 -1
- package/dist/types/modelShared/prop.d.ts +5 -0
- package/dist/types/redux/connectReduxDevTools.d.ts +17 -4
- package/dist/types/ref/Ref.d.ts +1 -1
- package/dist/types/treeUtils/sandbox.d.ts +1 -1
- package/dist/types/types/TypeChecker.d.ts +1 -1
- package/dist/types/types/arrayBased/typesArray.d.ts +1 -1
- package/dist/types/types/arrayBased/typesTuple.d.ts +1 -1
- package/dist/types/types/objectBased/typesArraySet.d.ts +1 -1
- package/dist/types/types/objectBased/typesDataModelData.d.ts +1 -1
- package/dist/types/types/objectBased/typesModel.d.ts +1 -1
- package/dist/types/types/objectBased/typesObject.d.ts +3 -3
- package/dist/types/types/objectBased/typesObjectMap.d.ts +1 -1
- package/dist/types/types/objectBased/typesRecord.d.ts +1 -1
- package/dist/types/types/primitiveBased/typesPrimitive.d.ts +1 -1
- package/dist/types/types/types.d.ts +3 -3
- package/dist/types/types/utility/typesCodec.d.ts +1 -6
- package/dist/types/types/utility/typesCodecCore.d.ts +1 -1
- package/dist/types/types/utility/typesOr.d.ts +1 -1
- package/dist/types/types/utility/typesRefinement.d.ts +1 -1
- package/dist/types/types/utility/typesSkipCheck.d.ts +1 -1
- package/dist/types/types/utility/typesTag.d.ts +1 -1
- package/dist/types/wrappers/ObjectMap.d.ts +2 -0
- package/dist/types/wrappers/asMap.d.ts +10 -6
- package/package.json +14 -12
- package/src/action/applyAction.ts +125 -114
- package/src/action/applyMethodCall.ts +37 -37
- package/src/action/isModelAction.ts +16 -16
- package/src/action/middleware.ts +137 -137
- package/src/action/modelFlow.ts +240 -240
- package/src/action/modelFlowPromiseGenerator.ts +127 -125
- package/src/action/pendingActions.ts +42 -42
- package/src/action/wrapInAction.ts +156 -156
- package/src/actionMiddlewares/actionSerialization/actionSerialization.ts +1 -1
- package/src/actionMiddlewares/actionSerialization/applySerializedAction.ts +172 -172
- package/src/actionMiddlewares/actionSerialization/arraySerializer.ts +20 -20
- package/src/actionMiddlewares/actionSerialization/dateSerializer.ts +17 -17
- package/src/actionMiddlewares/actionSerialization/mapSerializer.ts +42 -42
- package/src/actionMiddlewares/actionSerialization/objectPathSerializer.ts +50 -50
- package/src/actionMiddlewares/actionSerialization/objectSnapshotSerializer.ts +21 -21
- package/src/actionMiddlewares/actionSerialization/plainObjectSerializer.ts +24 -32
- package/src/actionMiddlewares/actionSerialization/primitiveSerializer.ts +51 -51
- package/src/actionMiddlewares/actionSerialization/setSerializer.ts +37 -37
- package/src/actionMiddlewares/actionTrackingMiddleware.ts +3 -3
- package/src/actionMiddlewares/onActionMiddleware.ts +2 -2
- package/src/actionMiddlewares/readonlyMiddleware.ts +2 -2
- package/src/actionMiddlewares/transactionMiddleware.ts +4 -4
- package/src/actionMiddlewares/undoMiddleware.ts +804 -796
- package/src/actionMiddlewares/utils.ts +2 -2
- package/src/computedTree/computedTree.ts +143 -143
- package/src/context/context.ts +1 -1
- package/src/dataModel/BaseDataModel.ts +194 -192
- package/src/dataModel/actions.ts +1 -1
- package/src/dataModel/newDataModel.ts +57 -57
- package/src/deepChange/index.ts +1 -1
- package/src/deepChange/onDeepChange.ts +244 -244
- package/src/frozen/Frozen.ts +146 -146
- package/src/globalConfig/globalConfig.ts +107 -107
- package/src/index.ts +24 -24
- package/src/model/BaseModel.ts +275 -273
- package/src/model/ModelConstructorOptions.ts +15 -15
- package/src/model/getModelMetadata.ts +51 -51
- package/src/model/index.ts +6 -6
- package/src/model/mixins.ts +288 -288
- package/src/model/newModel.ts +301 -301
- package/src/model/utils.ts +249 -249
- package/src/modelShared/modelDecorator.ts +260 -253
- package/src/modelShared/modelInfo.ts +50 -50
- package/src/modelShared/modelSymbols.ts +3 -3
- package/src/modelShared/newModel.ts +25 -25
- package/src/modelShared/prop.ts +713 -684
- package/src/modelShared/sharedInternalModel.ts +427 -417
- package/src/modelShared/utils.ts +24 -24
- package/src/parent/core.ts +65 -65
- package/src/parent/coreObjectChildren.ts +180 -180
- package/src/parent/detach.ts +47 -47
- package/src/parent/findParent.ts +1 -1
- package/src/parent/getChildrenObjects.ts +25 -25
- package/src/parent/path.ts +386 -381
- package/src/parent/setParent.ts +149 -149
- package/src/parent/walkTree.ts +1 -1
- package/src/patch/applyPatches.ts +187 -187
- package/src/patch/jsonPatch.ts +108 -108
- package/src/patch/patchRecorder.ts +6 -1
- package/src/redux/connectReduxDevTools.ts +39 -7
- package/src/redux/redux.ts +6 -2
- package/src/ref/Ref.ts +1 -1
- package/src/ref/core.ts +339 -345
- package/src/ref/customRef.ts +3 -3
- package/src/ref/rootRef.ts +100 -100
- package/src/rootStore/attachDetach.ts +89 -89
- package/src/rootStore/rootStore.ts +1 -1
- package/src/snapshot/SnapshotOf.ts +104 -104
- package/src/snapshot/SnapshotProcessingError.ts +45 -45
- package/src/snapshot/applySnapshot.ts +177 -181
- package/src/snapshot/cloneTreeValue.ts +30 -30
- package/src/snapshot/fromArraySnapshot.ts +27 -33
- package/src/snapshot/fromModelSnapshot.ts +70 -70
- package/src/snapshot/fromPlainObjectSnapshot.ts +47 -42
- package/src/snapshot/fromSnapshot.ts +198 -198
- package/src/snapshot/getSnapshot.ts +80 -79
- package/src/snapshot/index.ts +8 -8
- package/src/snapshot/internal.ts +230 -230
- package/src/snapshot/reconcileArraySnapshot.ts +70 -70
- package/src/snapshot/reconcileModelSnapshot.ts +155 -150
- package/src/snapshot/reconcilePlainObjectSnapshot.ts +72 -72
- package/src/snapshot/reconcileSnapshot.ts +88 -88
- package/src/standardActions/actions.ts +64 -64
- package/src/standardActions/objectActions.ts +60 -60
- package/src/transforms/ImmutableDate.ts +93 -93
- package/src/transforms/asMap.ts +50 -50
- package/src/transforms/asSet.ts +22 -22
- package/src/transforms/bigint.ts +20 -20
- package/src/transforms/date.ts +40 -40
- package/src/treeUtils/deepEquals.ts +55 -55
- package/src/treeUtils/sandbox.ts +6 -3
- package/src/tweaker/TweakerPriority.ts +9 -9
- package/src/tweaker/tweak.ts +4 -3
- package/src/tweaker/tweakArray.ts +530 -475
- package/src/tweaker/tweakFrozen.ts +42 -42
- package/src/tweaker/tweakModel.ts +1 -1
- package/src/tweaker/tweakPlainObject.ts +355 -355
- package/src/tweaker/typeChecking.ts +101 -101
- package/src/types/SnapshotTypeMismatchError.ts +46 -46
- package/src/types/TypeCheckError.ts +183 -184
- package/src/types/TypeChecker.ts +202 -185
- package/src/types/arrayBased/typesArray.ts +136 -137
- package/src/types/arrayBased/typesTuple.ts +138 -139
- package/src/types/createPerEntryCachedCheck.ts +166 -160
- package/src/types/index.ts +6 -6
- package/src/types/objectBased/typesArraySet.ts +133 -134
- package/src/types/objectBased/typesDataModelData.ts +203 -204
- package/src/types/objectBased/typesModel.ts +220 -221
- package/src/types/objectBased/typesObject.ts +308 -310
- package/src/types/objectBased/typesObjectMap.ts +142 -143
- package/src/types/objectBased/typesRecord.ts +156 -157
- package/src/types/objectBased/typesRef.ts +89 -89
- package/src/types/primitiveBased/typesEnum.ts +63 -63
- package/src/types/primitiveBased/typesPrimitive.ts +219 -220
- package/src/types/registerDefaultStandardTypeResolvers.ts +21 -21
- package/src/types/resolveTypeChecker.ts +78 -78
- package/src/types/schemas.ts +208 -208
- package/src/types/tProp.ts +277 -268
- package/src/types/typeCheck.ts +21 -21
- package/src/types/typeCheckErrorUtils.ts +23 -23
- package/src/types/types.ts +142 -142
- package/src/types/utility/typeToStoredData.ts +37 -37
- package/src/types/utility/typesBigInt.ts +15 -15
- package/src/types/utility/typesCodec.ts +41 -38
- package/src/types/utility/typesCodecCore.ts +178 -178
- package/src/types/utility/typesCodecSupport.ts +806 -806
- package/src/types/utility/typesDate.ts +27 -27
- package/src/types/utility/typesMap.ts +253 -222
- package/src/types/utility/typesOr.ts +263 -259
- package/src/types/utility/typesRefinement.ts +131 -125
- package/src/types/utility/typesSet.ts +110 -110
- package/src/types/utility/typesSkipCheck.ts +69 -70
- package/src/types/utility/typesTag.ts +88 -82
- package/src/types/utility/typesUnchecked.ts +40 -40
- package/src/utils/AnyFunction.ts +4 -4
- package/src/utils/ModelPool.ts +59 -59
- package/src/utils/chainFns.ts +18 -18
- package/src/utils/decorators.ts +274 -274
- package/src/utils/errorDiagnostics.ts +204 -204
- package/src/utils/index.ts +443 -372
- package/src/wrappers/ArraySet.ts +156 -156
- package/src/wrappers/ObjectMap.ts +155 -136
- package/src/wrappers/asMap.ts +65 -30
- package/src/wrappers/asSet.ts +3 -3
package/src/types/tProp.ts
CHANGED
|
@@ -1,268 +1,277 @@
|
|
|
1
|
-
import {
|
|
2
|
-
|
|
3
|
-
|
|
4
|
-
|
|
5
|
-
|
|
6
|
-
|
|
7
|
-
|
|
8
|
-
|
|
9
|
-
|
|
10
|
-
|
|
11
|
-
|
|
12
|
-
|
|
13
|
-
|
|
14
|
-
|
|
15
|
-
|
|
16
|
-
|
|
17
|
-
|
|
18
|
-
|
|
19
|
-
|
|
20
|
-
|
|
21
|
-
|
|
22
|
-
|
|
23
|
-
|
|
24
|
-
|
|
25
|
-
|
|
26
|
-
|
|
27
|
-
type
|
|
28
|
-
|
|
29
|
-
>
|
|
30
|
-
|
|
31
|
-
|
|
32
|
-
type
|
|
33
|
-
|
|
34
|
-
|
|
35
|
-
|
|
36
|
-
|
|
37
|
-
|
|
38
|
-
|
|
39
|
-
|
|
40
|
-
|
|
41
|
-
|
|
42
|
-
|
|
43
|
-
|
|
44
|
-
|
|
45
|
-
|
|
46
|
-
|
|
47
|
-
|
|
48
|
-
|
|
49
|
-
|
|
50
|
-
|
|
51
|
-
|
|
52
|
-
|
|
53
|
-
|
|
54
|
-
|
|
55
|
-
|
|
56
|
-
|
|
57
|
-
|
|
58
|
-
|
|
59
|
-
|
|
60
|
-
|
|
61
|
-
|
|
62
|
-
|
|
63
|
-
|
|
64
|
-
*
|
|
65
|
-
*
|
|
66
|
-
*
|
|
67
|
-
*
|
|
68
|
-
*
|
|
69
|
-
|
|
70
|
-
|
|
71
|
-
|
|
72
|
-
|
|
73
|
-
*
|
|
74
|
-
|
|
75
|
-
|
|
76
|
-
|
|
77
|
-
|
|
78
|
-
*
|
|
79
|
-
*
|
|
80
|
-
*
|
|
81
|
-
*
|
|
82
|
-
*
|
|
83
|
-
|
|
84
|
-
|
|
85
|
-
|
|
86
|
-
|
|
87
|
-
*
|
|
88
|
-
|
|
89
|
-
|
|
90
|
-
|
|
91
|
-
|
|
92
|
-
*
|
|
93
|
-
*
|
|
94
|
-
*
|
|
95
|
-
*
|
|
96
|
-
*
|
|
97
|
-
|
|
98
|
-
|
|
99
|
-
|
|
100
|
-
|
|
101
|
-
*
|
|
102
|
-
|
|
103
|
-
|
|
104
|
-
|
|
105
|
-
|
|
106
|
-
*
|
|
107
|
-
*
|
|
108
|
-
*
|
|
109
|
-
*
|
|
110
|
-
*
|
|
111
|
-
*
|
|
112
|
-
*
|
|
113
|
-
*
|
|
114
|
-
*
|
|
115
|
-
|
|
116
|
-
|
|
117
|
-
|
|
118
|
-
|
|
119
|
-
|
|
120
|
-
|
|
121
|
-
|
|
122
|
-
|
|
123
|
-
|
|
124
|
-
|
|
125
|
-
|
|
126
|
-
|
|
127
|
-
*
|
|
128
|
-
*
|
|
129
|
-
*
|
|
130
|
-
*
|
|
131
|
-
*
|
|
132
|
-
*
|
|
133
|
-
*
|
|
134
|
-
*
|
|
135
|
-
*
|
|
136
|
-
|
|
137
|
-
|
|
138
|
-
|
|
139
|
-
|
|
140
|
-
|
|
141
|
-
|
|
142
|
-
|
|
143
|
-
|
|
144
|
-
|
|
145
|
-
|
|
146
|
-
|
|
147
|
-
|
|
148
|
-
*
|
|
149
|
-
*
|
|
150
|
-
*
|
|
151
|
-
*
|
|
152
|
-
*
|
|
153
|
-
*
|
|
154
|
-
*
|
|
155
|
-
*
|
|
156
|
-
*
|
|
157
|
-
*
|
|
158
|
-
|
|
159
|
-
|
|
160
|
-
|
|
161
|
-
|
|
162
|
-
|
|
163
|
-
|
|
164
|
-
|
|
165
|
-
|
|
166
|
-
|
|
167
|
-
|
|
168
|
-
|
|
169
|
-
|
|
170
|
-
*
|
|
171
|
-
*
|
|
172
|
-
*
|
|
173
|
-
*
|
|
174
|
-
*
|
|
175
|
-
*
|
|
176
|
-
*
|
|
177
|
-
*
|
|
178
|
-
*
|
|
179
|
-
|
|
180
|
-
|
|
181
|
-
|
|
182
|
-
|
|
183
|
-
|
|
184
|
-
|
|
185
|
-
|
|
186
|
-
|
|
187
|
-
|
|
188
|
-
|
|
189
|
-
|
|
190
|
-
|
|
191
|
-
*
|
|
192
|
-
*
|
|
193
|
-
*
|
|
194
|
-
*
|
|
195
|
-
*
|
|
196
|
-
*
|
|
197
|
-
*
|
|
198
|
-
|
|
199
|
-
|
|
200
|
-
|
|
201
|
-
|
|
202
|
-
*
|
|
203
|
-
|
|
204
|
-
|
|
205
|
-
|
|
206
|
-
|
|
207
|
-
*
|
|
208
|
-
*
|
|
209
|
-
*
|
|
210
|
-
*
|
|
211
|
-
*
|
|
212
|
-
*
|
|
213
|
-
*
|
|
214
|
-
|
|
215
|
-
|
|
216
|
-
|
|
217
|
-
)
|
|
218
|
-
|
|
219
|
-
|
|
220
|
-
|
|
221
|
-
|
|
222
|
-
|
|
223
|
-
|
|
224
|
-
|
|
225
|
-
|
|
226
|
-
|
|
227
|
-
|
|
228
|
-
|
|
229
|
-
|
|
230
|
-
|
|
231
|
-
|
|
232
|
-
|
|
233
|
-
|
|
234
|
-
|
|
235
|
-
|
|
236
|
-
|
|
237
|
-
|
|
238
|
-
|
|
239
|
-
const
|
|
240
|
-
|
|
241
|
-
|
|
242
|
-
|
|
243
|
-
|
|
244
|
-
|
|
245
|
-
|
|
246
|
-
|
|
247
|
-
|
|
248
|
-
|
|
249
|
-
|
|
250
|
-
|
|
251
|
-
|
|
252
|
-
|
|
253
|
-
const
|
|
254
|
-
|
|
255
|
-
|
|
256
|
-
|
|
257
|
-
|
|
258
|
-
|
|
259
|
-
|
|
260
|
-
|
|
261
|
-
|
|
262
|
-
|
|
263
|
-
|
|
264
|
-
|
|
265
|
-
|
|
266
|
-
|
|
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
|
+
}
|
package/src/types/typeCheck.ts
CHANGED
|
@@ -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
|
+
}
|