- Notifications
You must be signed in to change notification settings - Fork638
Typescript Union of Reference & Model#1999
Uh oh!
There was an error while loading.Please reload this page.
Uh oh!
There was an error while loading.Please reload this page.
-
Hi! I have a question about an issue I ran into when I have a union of reference and a model and I try to access an action that they inherited from a Base Model. To explain it more, I have a codesandbox here:https://codesandbox.io/s/inspiring-snyder-yregis?file=/src/Sample.ts When I am accessing |
BetaWas this translation helpful?Give feedback.
All reactions
Hey@sethdumaguin - sorry it took so long for folks to get back to you.
I'm not precisely sure what's going on here, but I wonder if this is an issue where TypeScript can't appropriately infer the type from thetypes.reference call. I was able to remove the linting error by making these changes:
import { types } from "mobx-state-tree";const Base = types.model("Base", { name: "" }).actions((self) => ({ setName: (name: string) => { self.name = name; }}));const Foo = types.compose( "Foo", Base, types.model({ type: "Foo" }));const Bar = types.compose( "Bar", Base, types.model({ id: types.identifier, type: "Bar" }));const Tree = types .model("Tr…Replies: 1 comment 1 reply
-
Hey@sethdumaguin - sorry it took so long for folks to get back to you. I'm not precisely sure what's going on here, but I wonder if this is an issue where TypeScript can't appropriately infer the type from the Here's the code sandbox:https://codesandbox.io/s/mobx-state-tree-todolist-forked-0lwr9n?file=/index.ts The only change is explicitly using baz:types.union(types.reference(Bar),Foo)astypeofBar|typeofFoo I'm not sure if this is the fully correct way to do it, and I definitely don't love having to use Is that helpful? Did you end up finding some other solution since posting this? Would love to get a sense of where you landed on this one. |
BetaWas this translation helpful?Give feedback.
All reactions
-
I'm gonna mark this as an answer since we didn't hear back. There are plenty of sharp edges around TS and MST, and we're working to improve that, but I think this is a reasonable workaround. |
BetaWas this translation helpful?Give feedback.