Mobx-state-tree: compose chain-able

Created on 11 Feb 2018  路  3Comments  路  Source: mobxjs/mobx-state-tree

With TypeScript I've run into a current limitation of compose function being defined with 2 additional models only. I definitely would like to compose more models together as it supports separation of concerns and easier testability.

I don't think that proper solution here is to keep adding overloads when someone needs more. What I want to suggest is basically expose cloneAndEnhance function on the model (probably renamed to something else) and accepting a single model type only. That will remove the problematic limitation with Typescript and allow for more robust composition approach.

enhancement helPR welcome

Most helpful comment

yes sorry - misread your question in the beginning :)

you're looking for something like this right?
i guess that'd be quite doable.
@mweststrate @mattiamanzati

const TestModel = types.model('Test', { testProperty: types.string })
const AnotherModel = types.model('Another', { someProp: types.string })
const ComposedModel = TestModel.compose('Composed', AnotherModel).compose(...

All 3 comments

I don't see how that's related to the issue at hand :) What I want to simply do is types.compose(Model1, Model2, Model3 ... ModelN). The named is just for ... well naming models, it has nothing to do with an actual composition or is I missing something here?

There is also mention of union method which could serve to this needs, but that kinda suffers with a similar problem, there are overloads for 10 models at max.

yes sorry - misread your question in the beginning :)

you're looking for something like this right?
i guess that'd be quite doable.
@mweststrate @mattiamanzati

const TestModel = types.model('Test', { testProperty: types.string })
const AnotherModel = types.model('Another', { someProp: types.string })
const ComposedModel = TestModel.compose('Composed', AnotherModel).compose(...

on the todo list now.

Was this page helpful?
0 / 5 - 0 ratings

Related issues

mshibl picture mshibl  路  3Comments

lishine picture lishine  路  4Comments

xgenvn picture xgenvn  路  3Comments

EricForgy picture EricForgy  路  4Comments

LukaszAmroz picture LukaszAmroz  路  3Comments