Skip to content

Commit f8bb147

Browse files
fix: revert 2218 to fix ts excessively deep types (#2234)
1 parent 959c5db commit f8bb147

File tree

3 files changed

+38
-105
lines changed

3 files changed

+38
-105
lines changed

__tests__/core/2230.test.ts

Lines changed: 25 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,4 @@
11
//github.com/mobxjs/mobx-state-tree/issues/2230
2-
32
import { describe, test } from "bun:test"
43
import { types, Instance } from "../../src/index"
54

@@ -150,6 +149,31 @@ describe("2230 - type instantiation is excessively deep and possibly infinite",
150149
return self.prop41
151150
}
152151
}))
152+
.actions((self: IAction4) => ({
153+
getProp51(): string {
154+
return self.prop51
155+
}
156+
}))
157+
.actions((self: IAction4) => ({
158+
getProp61(): string {
159+
return self.prop61
160+
}
161+
}))
162+
.actions((self: IAction4) => ({
163+
getProp71(): string {
164+
return self.prop71
165+
}
166+
}))
167+
.actions((self: IAction4) => ({
168+
getProp81(): string {
169+
return self.prop81
170+
}
171+
}))
172+
.actions((self: IAction4) => ({
173+
getProp91(): string {
174+
return self.prop91
175+
}
176+
}))
153177
interface IAction5 extends Instance<typeof Action5> {}
154178
})
155179
})

__tests__/core/type-system.test.ts

Lines changed: 0 additions & 81 deletions
Original file line numberDiff line numberDiff line change
@@ -1417,84 +1417,3 @@ test("#1627 - union dispatch function is typed", () => {
14171417
types.null
14181418
)
14191419
})
1420-
1421-
test("#2216 - should respect optionality when extending another type", () => {
1422-
const Base = types.model("ErrorStore", { value: types.string }).extend(self => ({
1423-
actions: {
1424-
setValue(value?: string): boolean {
1425-
self.value = value || "test"
1426-
return true
1427-
},
1428-
1429-
setAnotherValue(value?: string): boolean {
1430-
self.value = value || "test"
1431-
return true
1432-
}
1433-
},
1434-
views: {
1435-
get spam(): string {
1436-
return self.value
1437-
},
1438-
1439-
get eggs(): string {
1440-
return self.value
1441-
}
1442-
},
1443-
state: {
1444-
anotherValue: "test" as string,
1445-
soManyValues: "test" as string
1446-
}
1447-
}))
1448-
1449-
const Extended = Base.named("Extended")
1450-
.props({
1451-
value: "test"
1452-
})
1453-
.extend(self => ({
1454-
actions: {
1455-
setValue(value: string): number {
1456-
self.value = value
1457-
return value.length
1458-
}
1459-
},
1460-
views: {
1461-
get spam(): boolean {
1462-
return !!self.value
1463-
}
1464-
},
1465-
state: {
1466-
anotherValue: "test" as string | undefined
1467-
}
1468-
}))
1469-
.actions(self => ({
1470-
setAnotherValue(value: string): number {
1471-
self.value = value
1472-
return value.length
1473-
}
1474-
}))
1475-
.views(self => ({
1476-
get eggs(): boolean {
1477-
return !!self.value
1478-
}
1479-
}))
1480-
.volatile(self => ({
1481-
soManyValues: "test" as string | undefined
1482-
}))
1483-
1484-
type InputSnapshot = SnapshotIn<typeof Extended>
1485-
type InstanceType = Instance<typeof Extended>
1486-
1487-
assertTypesEqual(_ as InputSnapshot, _ as { value?: string })
1488-
assertTypesEqual(
1489-
_ as InstanceType,
1490-
_ as {
1491-
value: string
1492-
setValue(value: string): number
1493-
setAnotherValue(value: string): number
1494-
spam: boolean
1495-
eggs: boolean
1496-
anotherValue: string | undefined
1497-
soManyValues: string | undefined
1498-
}
1499-
)
1500-
})

src/types/complex-types/model.ts

Lines changed: 13 additions & 23 deletions
Original file line numberDiff line numberDiff line change
@@ -73,11 +73,6 @@ export interface ModelPropertiesDeclaration {
7373
[key: string]: ModelPrimitive | IAnyType
7474
}
7575

76-
/** intersect two object types, but omit keys of B from A before doing so */
77-
type OmitMerge<A, B> = {
78-
[K in keyof A as K extends keyof B ? never : K]: A[K]
79-
} & B
80-
8176
/**
8277
* Unmaps syntax property declarations to a map of { propName: IType }
8378
*
@@ -205,28 +200,23 @@ export interface IModelType<
205200
// so it is recommended to use pre/post process snapshot after all props have been defined
206201
props<PROPS2 extends ModelPropertiesDeclaration>(
207202
props: PROPS2
208-
): IModelType<
209-
OmitMerge<PROPS, ModelPropertiesDeclarationToProperties<PROPS2>>,
210-
OTHERS,
211-
CustomC,
212-
CustomS
213-
>
203+
): IModelType<PROPS & ModelPropertiesDeclarationToProperties<PROPS2>, OTHERS, CustomC, CustomS>
214204

215-
views<V extends AnyObject>(
205+
views<V extends Object>(
216206
fn: (self: Instance<this>) => V
217-
): IModelType<PROPS, OmitMerge<OTHERS, V>, CustomC, CustomS>
207+
): IModelType<PROPS, OTHERS & V, CustomC, CustomS>
218208

219209
actions<A extends ModelActions>(
220210
fn: (self: Instance<this>) => A
221-
): IModelType<PROPS, OmitMerge<OTHERS, A>, CustomC, CustomS>
211+
): IModelType<PROPS, OTHERS & A, CustomC, CustomS>
222212

223-
volatile<VS extends AnyObject>(
224-
fn: (self: Instance<this>) => VS
225-
): IModelType<PROPS, OmitMerge<OTHERS, VS>, CustomC, CustomS>
213+
volatile<TP extends object>(
214+
fn: (self: Instance<this>) => TP
215+
): IModelType<PROPS, OTHERS & TP, CustomC, CustomS>
226216

227-
extend<A extends ModelActions = {}, V extends AnyObject = {}, VS extends AnyObject = {}>(
217+
extend<A extends ModelActions = {}, V extends Object = {}, VS extends Object = {}>(
228218
fn: (self: Instance<this>) => { actions?: A; views?: V; state?: VS }
229-
): IModelType<PROPS, OmitMerge<OTHERS, A & V & VS>, CustomC, CustomS>
219+
): IModelType<PROPS, OTHERS & A & V & VS, CustomC, CustomS>
230220

231221
preProcessSnapshot<NewC = ModelCreationType2<PROPS, CustomC>>(
232222
fn: (snapshot: NewC) => WithAdditionalProperties<ModelCreationType2<PROPS, CustomC>>
@@ -477,7 +467,7 @@ export class ModelType<
477467
return this.cloneAndEnhance({ properties })
478468
}
479469

480-
volatile<TP extends AnyObject>(fn: (self: Instance<this>) => TP) {
470+
volatile<TP extends object>(fn: (self: Instance<this>) => TP) {
481471
if (typeof fn !== "function") {
482472
throw new MstError(
483473
`You passed an ${typeof fn} to volatile state as an argument, when function is expected`
@@ -514,7 +504,7 @@ export class ModelType<
514504
})
515505
}
516506

517-
extend<A extends ModelActions = {}, V extends AnyObject = {}, VS extends AnyObject = {}>(
507+
extend<A extends ModelActions = {}, V extends Object = {}, VS extends Object = {}>(
518508
fn: (self: Instance<this>) => { actions?: A; views?: V; state?: VS }
519509
) {
520510
const initializer = (self: Instance<this>) => {
@@ -531,15 +521,15 @@ export class ModelType<
531521
return this.cloneAndEnhance({ initializers: [initializer] })
532522
}
533523

534-
views<V extends AnyObject>(fn: (self: Instance<this>) => V) {
524+
views<V extends Object>(fn: (self: Instance<this>) => V) {
535525
const viewInitializer = (self: Instance<this>) => {
536526
this.instantiateViews(self, fn(self))
537527
return self
538528
}
539529
return this.cloneAndEnhance({ initializers: [viewInitializer] })
540530
}
541531

542-
private instantiateViews(self: this["T"], views: AnyObject): void {
532+
private instantiateViews(self: this["T"], views: Object): void {
543533
// check views return
544534
if (!isPlainObject(views)) {
545535
throw new MstError(`views initializer should return a plain object containing views`)

0 commit comments

Comments
 (0)