xaviergonz / mobx-keystone

A MobX powered state management solution based on data trees with first class support for Typescript, support for snapshots, patches and much more

Home Page:https://mobx-keystone.js.org

Geek Repo:Geek Repo

Github PK Tool:Github PK Tool

How to set the type of idProp?

fineup opened this issue · comments

commented

After customizing the id according to the method of Setting an ID property, the similar default returned under typescript is string. How to specify the type, such as Custom-${string}

idProp.typedAs<T>() added in v1.1.0

unit test

test("idProp.typedAs", () => {
  type Id = `custom-${string}`
  @testModel("M")
  class M extends Model({
    id: idProp.typedAs<Id>(),
  }) {}

  const m = new M({ id: "custom-1" })
  assert(m.id, _ as Id)
  assert(_ as SnapshotInOf<M>["id"], _ as Id | undefined)
  assert(_ as SnapshotOutOf<M>["id"], _ as Id)
  assert(m.$modelId, _ as Id)
})

Closing since it now should be supported as shown above.