- Notifications
You must be signed in to change notification settings - Fork638
Uh oh!
There was an error while loading.Please reload this page.
Uh oh!
There was an error while loading.Please reload this page.
-
So this simple code: typeFoo={foo:string}constMyModelFoo=types.model<Foo>('Foo',{foo:types.string,}) triggers the following error: What I do wrong? This looks like a bug. |
BetaWas this translation helpful?Give feedback.
All reactions
👍 1
The typing here is probably a bit confusing, but I'd recommend using your type for instances, not the model itself:
type Foo = { foo: string}const MyModelFoo = types.model('Foo', { foo: types.string,})const fooInstance: Foo = MyModelFoo.create({ foo: "test" })The problem in your example is that the generic argument fortypes.model describes the structure of the properties and not the instance.{ foo: types.string } does not match{ foo: string }, hence the error you're seeing. Itwould match{ foo: "abc" }, which would actually be the same as{ foo: types.optional(types.string, "abc") }.
In general I'd recommend always letting TypeScript derive the generic arguments fortypes.…
Replies: 1 comment 8 replies
-
The typing here is probably a bit confusing, but I'd recommend using your type for instances, not the model itself: The problem in your example is that the generic argument for In general I'd recommend always letting TypeScript derive the generic arguments for |
BetaWas this translation helpful?Give feedback.
All reactions
-
@OnkelTem everything looks to be working as expected in the TS playground links you provided. In the first one, there are no type errors as expected, since I'm also seeing intellisense when creating a model, and also when referencing properties on a created model: Do you have a more complete example where things aren't working? |
BetaWas this translation helpful?Give feedback.
All reactions
-
Intellisense works for an existing model, while I speak about creatingthe model itself. Instead we're given with a TypeScript API, where and get nothing, because model isn't typed.
Yeah, but that's a result of an investigation and not what is displayed in the error message. |
BetaWas this translation helpful?Give feedback.
All reactions
Uh oh!
There was an error while loading.Please reload this page.
Uh oh!
There was an error while loading.Please reload this page.
-
Oh, I think I understand what you're asking now. You type t.model('Foo',{ And then ask for intellisense to show you options. There aren't meant to be options there by default, but you happen to already have an existing interface and want to have MST adhere to that. I don't think there's a natural solution that MST can give you (maybe you have some suggestions?), but I can see two options that you could use without any changes to MST: Use The type of a properties blob is pretty complicated, so I don't think you can easily adapt some arbitrary interface to it. If you happen to only be mapping keys to primitive values, you could probably do something similar to what we do internally (which MST could expose): mobx-state-tree/src/types/complex-types/model.ts Lines 76 to 96 inab45ead
Alternatively, if you only care about the keys, you could do something like this: types.model("Foo",{}satisfiesRecord<keyofFoo,any>) That's kind of gross though, so maybe this would be better: import{types,IType}from"mobx-state-tree";typeFoo={foo:string;};constMyModelFoo=types.model("Foo",{foo:types.string,})satisfiesIType<any,Foo,any>; Adjust your type assertions If your interface is meant to match an instance/snapshot of a model, which I'm guessing it is given your playground links, I would recommend something close to what you had in TS playground: import{types,SnapshotOut}from"mobx-state-tree";typeFoo={foo:number;};constMyModelFoo=types.model("Foo",{foo:types.string,});typeFooDerived=SnapshotOut<typeofMyModelFoo>typeTest<TextendsFoo>="yes";type_=Test<FooDerived> That will give you something a little more meaningful in the error message at the cost of having to write these out for every model: |
BetaWas this translation helpful?Give feedback.
All reactions
❤️ 1
-
I see, Jason! Many thanks :) As for the ideas - yeah, I've got one:#2239 :) |
BetaWas this translation helpful?Give feedback.
All reactions
❤️ 1
-
@thegedge really nailed it here, but I understand where you're coming from,@OnkelTem. I'll add that I've seen the I'll read through#2239 and think it over. But as things stand, that's my best recommendation for you with MST as-is. |
BetaWas this translation helpful?Give feedback.


