mobx-state-tree
mobx-state-tree copied to clipboard
TypeScript: how to get type of model properties without internal fields?
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"; }; }
As a workaround I applied Omit< MyModelSnapshot , symbol>
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
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!