mobx-state-tree
mobx-state-tree copied to clipboard
Error with reference types after updating to latest version
Bug report
- [x ] I've checked documentation and searched for existing issues
- [x ] I've made sure my project is based on the latest MST version
- [ x] Fork this code sandbox or another minimal reproduction.
Sandbox link or minimal reproduction code https://codesandbox.io/s/mobx-state-tree-todolist-forked-3060p?file=/index.ts Note: You will have to wait for a few seconds for the error to be highlighted in the index.ts file
Describe the expected behavior I have a working application where I have the following code:
export const ProjectStore = types.model("ProjectStore", {
projects: types.array(Project),
currentProject: types.maybeNull(types.reference(Project))
});
export const RootStore = types.model("RootStore", {
projectStore: ProjectStore
});
const rootStore = RootStore.create({
projectStore: ProjectStore.create()
});
Describe the observed behavior After upgrading to the latest version of MST (3.17.2) I now have a type checking error at:
projectStore: ProjectStore.create()
which is caused by line:
currentProject: types.maybeNull(types.reference(Project))
The error message is:
Types of property 'currentProject' are incompatible. Type '({ id: number; name: string; imageUrl: string | null; } & NonEmptyObject & IStateTreeNode<IMaybeNull<IReferenceType<IModelType<{ id: IOptionalIType<ISimpleType<number>, [undefined]>; name: IType<...>; imageUrl: IMaybeNull<...>; }, {}, _NotCustomized, _NotCustomized>>>>) | null' is not assignable to type 'string | number | null | undefined'. Type '{ id: number; name: string; imageUrl: string | null; } & NonEmptyObject & IStateTreeNode<IMaybeNull<IReferenceType<IModelType<{ id: IOptionalIType<ISimpleType<number>, [undefined]>; name: IType<...>; imageUrl: IMaybeNull<...>; }, {}, _NotCustomized, _NotCustomized>>>>' is not assignable to type 'string | number | null | undefined'. Type '{ id: number; name: string; imageUrl: string | null; } & NonEmptyObject & IStateTreeNode<IMaybeNull<IReferenceType<IModelType<{ id: IOptionalIType<ISimpleType<number>, [undefined]>; name: IType<...>; imageUrl: IMaybeNull<...>; }, {}, _NotCustomized, _NotCustomized>>>>' is not assignable to type 'number'.
I'm having this problem as well and my current workaround is:
const rootStore = RootStore.create({
// @ts-ignore
projectStore: ProjectStore.create()
});
It's not great but it at least allows me to run the rest of my code while waiting for this to be fixed.
Hey @peejayess - sorry it's been a while since there was activity here. Are you still using MST? Have you upgraded to v5 yet? If so, is this still an issue for you?
We are gathering some info to try and improve our TypeScript usage here. I'm going to label this as a TypeScript issue so we can get it prioritized for improvement.
Thanks!