mobx-state-tree icon indicating copy to clipboard operation
mobx-state-tree copied to clipboard

TypeScript: how to get type of model properties without internal fields?

Open DnEgorWeb opened this issue 5 years ago • 3 comments

When I create a Model Snapshot (typeof Model.SnapshotType) additionally to the fields of my model I get NonEmptyObject. My question is how to get just my fields? I.g. I have a Model:

MyModel = types.model('MyModel', {
  field_one: types.maybeNull(types.string),
  field_two: types.maybeNull(types.string),
})

export type MyModelType = typeof MyModel.Type
export type MyModelSnapshot = typeof MyModel.SnapshotType

Then if I want to type an object which contains properties from the model I do the following:

const myObject: Record<
  keyof MyModelSnapshot,
  { firstValue: string; secondValue: string; }
>  = {
  field_one: { firstValue: 'mock'; secondValue: 'mock; },
  field_two: { firstValue: 'mock'; secondValue: 'mock; },
}

And I get TS error on this object Property '[$nonEmptyObject]' is missing in type { field_one: { firstValue: "mock"; secondValue: "mock"; }; field_two: { firstValue: "mock"; secondValue: "mock"; }; }

DnEgorWeb avatar May 17 '20 16:05 DnEgorWeb

As a workaround I applied Omit< MyModelSnapshot , symbol>

DnEgorWeb avatar May 18 '20 05:05 DnEgorWeb

thanks so much for posting this fix!

just a note: I had a similar question here https://github.com/mobxjs/mobx-state-tree/discussions/1699

cmdcolin avatar Dec 10 '21 02:12 cmdcolin

Hey @DnEgorWeb - thanks for putting this issue together and posting your workaround.

I think this is part of an underlying issue with the MobX-State-Tree TypeScript typings, so I'm going to label it as such and keep this open while the maintainers put together some priorities.

If you're interested in pitching in on the effort, I'd be happy to chat. But it's been a few years here so no worries if it's not on your radar anymore. We appreciate what you've already done here for us!

coolsoftwaretyler avatar Jun 30 '23 20:06 coolsoftwaretyler