Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Circular type references with TS 3.5 #1341

Closed
danielkcz opened this issue Jul 13, 2019 · 4 comments
Closed

Circular type references with TS 3.5 #1341

danielkcz opened this issue Jul 13, 2019 · 4 comments

Comments

@danielkcz
Copy link
Contributor

Bug report

Sandbox link or minimal reproduction code
https://github.com/FredyC/mobx-state-tree-ts3.5

Describe the expected behavior
I want to be able to use BaseModel that gives me a reference to the root model properly typed.

import { getRoot, types } from "mobx-state-tree"

import { TRootModel } from "./RootModel"

export const BaseModel = types.model().views(self => ({
  get root(): TRootModel {
    return getRoot<TRootModel>(self)
  }
}))

Describe the observed behavior
image

The same code works with TS < 3.4 (currently using 3.2 with it).

@mweststrate
Copy link
Member

mweststrate commented Jul 15, 2019

The second error gives the hint here: trying to infer the type of version makes it endlessly circular for typescript. Adding a type annotation at this point solves the problem, as it breaks the dependency of needing to know the type of system.version to determine the type of UserModel, which in turn needs to be know to determine the type of BaseModel in the first place:

get version(): string { /*etc */.


Fur future readers, the full source code was (I threw it into one file, otherwise I got lost in reading this, as the terms are meaningless to me):

import { types, Instance, SnapshotIn, getRoot } from 'mobx-state-tree'

export const BaseModel = types.model().views(self => ({
  get root(): TRootModel {
    return getRoot<TRootModel>(self)
  }
}))

export const SystemModel = BaseModel.named('System').props({
  version: types.frozen('1')
})

export interface TSystemModel extends Instance<typeof SystemModel> {}
export interface TSystemModelProps extends SnapshotIn<typeof SystemModel> {}

export const UserModel = BaseModel.named("User")
  .props({
    name: types.optional(types.string, "unknown")
  })
  .volatile(() => ({
    age: types.number
  }))
  .views(self => ({
    get version(): string /* added this annotation to break the circle */ {
      return self.root.system.version
    }
  }))

export interface TUserModel extends Instance<typeof UserModel> {}
export interface TUserModelProps extends SnapshotIn<typeof UserModel> {}

export const RootModel = types
  .model("Root")
  .props({
    system: types.optional(SystemModel, {}),
    user: types.optional(UserModel, {})
  })
  .views(self => ({
    get agedVersion() {
      return `${self.system.version} - ${self.user.age}`
    }
  }))

export interface TRootModel extends Instance<typeof RootModel> {}
export interface TRootModelProps extends SnapshotIn<typeof RootModel> {}

@danielkcz
Copy link
Contributor Author

danielkcz commented Jul 15, 2019

Well, that's not very nice :( I wonder why it worked with 3.4. Is 3.5 somehow stricter?

It's fairly inconvenient to by double typing things like that, kinda beats the reason for a typed models.

Do you possibly have some tips if something like that can be structured differently to avoid this problem?

@mweststrate
Copy link
Member

mweststrate commented Jul 15, 2019 via email

@lock
Copy link

lock bot commented Jan 14, 2020

This thread has been automatically locked since there has not been any recent activity after it was closed. Please open a new issue for related bugs or questions.

@lock lock bot locked as resolved and limited conversation to collaborators Jan 14, 2020
Sign up for free to subscribe to this conversation on GitHub. Already have an account? Sign in.
Labels
None yet
Projects
None yet
Development

No branches or pull requests

2 participants