mobxjs / mobx-state-tree

Full-featured reactive state management without the boilerplate
https://mobx-state-tree.js.org/
MIT License
6.94k stars 640 forks source link

Root Model complex cause inferred type error(ts7056) #1956

Closed uncledu closed 2 years ago

uncledu commented 2 years ago

Question how to solve the ts7056 error, can't build project now.

in my project, i have defined a root model(contains serveral sub model), seems it's so big that typescript compiler told me that it's too big, also cause build failure.

image image

uncledu commented 2 years ago

Find a solution for this issue, i'll wirte the solution here for anyone face the same situation.

const SubModel = types.model(...)
const DemoModel = types.model('DemoModel', {
  id: types.identifier,
  largeSubModel: types.optional(types.late(():IAnyModel=> SubModel))
}).views((self)=>{})
.actions((self)=>{})

for example, make the large sub model as up showing late, it'll turn it to be any type.

then, define a new interface as follow

interface IDemoModelReal extends Omit<DemoModel, 'largeSubModel' > {
  largeSubModel: SubModel;
}
const DemoModelTreeInsrtance = DemoModel.create({});

// export like this, will make the type correct

export DemoModelTreeInstance as IDemoModelReal