Mobx-state-tree: Using mst model structure as a type

Created on 16 Oct 2018  路  10Comments  路  Source: mobxjs/mobx-state-tree

I have:

  • [V] A conceptual question.

    • [V] I've checked documentation and searched for existing issues

    • [ V] I tried the spectrum channel first

Hey,
Lets assume i have a an "EventModel" and i wish to use this model as a type outside the scope of mst and inside a function of some sort with typescript. is there a way of doing it without typing an identical interface to eventModel?

for further clarification, if i have a model as follow


const A = types.model(
a: types.string,
b: types.number
)

is there a way to generate the identical copy of a none mst model
as follow from the mst model

interface B{
a: string,
b: number
}
question should be ready to close

Most helpful comment

interface B extends Instance<typeof A> {}
where instance has to be imported from mobx-state-tree

All 10 comments

interface B extends Instance<typeof A> {}
where instance has to be imported from mobx-state-tree

@xaviergonz Is there any possibility to transform interfaces to mst models? In other word, can I define a mst model with interface but mst types?

you'd need to create a tool that uses the typescript parser to parse the abstract tree of the interface and then generate a js file that generates the appropriate mst code out of it

actually that could be a cool idea for a typescript transformer, but still you'd loose optional defaults

I met some trouble when I was trying to transform type into interface, can you tell me what's wrong?
This is the codesandbox, please look at the schema.ts.

that's because the object your are declaring is actually a SnapshotOut, not an Instance

in the docs there are more details about the differences between SnapshotIn, SnapshotOut and Instance

thanks for the help

no problem 馃榿

Was this page helpful?
0 / 5 - 0 ratings