How to use a typescript type as a property type inside Mobx State tree model

1.2k Views Asked by At

I am using a Qraphql code generator that generates the schema types in typescript and would like to use those types inside the model.

My question is how can I use a typescript type inside the Mobx state tree model like this

type AssignedFilter = {
  id: number
  name: string
  email: string
}

const SearchStore = types
  .model('Search', {
    text: '',
    assigned: // how to use the AssignedFilter type here??
  })
1

There are 1 best solutions below

4
Tholle On

A type is a pure TypeScript concept, so it's not possible to use a type to define what MST type a property should be.

You can however do it the other way around and create a type from your MST models, if that suits your use case.

Example

const AssignedModel = types.model('Assigned', {
  id: types.identifierNumber,
  name: types.string,
  email: types.string
});

const SearchStore = types.model("Search", {
  text: "",
  assigned: AssignedModel
});

type AssignedFilter = SnapshotIn<typeof AssignedModel>;