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

Exported variable 'Todo' has or is using 'IModelType' ... but cannot be named. in Typescript #426

Closed
mikecann opened this issue Oct 6, 2017 · 10 comments

Comments

@mikecann
Copy link
Contributor

mikecann commented Oct 6, 2017

A simple example taken from the MST docs:

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

export const Todo = types.model({
    title: types.string
})
.named("Todo")
.actions(self => ({
    setTitle(v: string) {
        self.title = v
    }
}))

export type ITodo = typeof Todo.Type

With tsconfig.json:

{
  "compilerOptions": {
    "outDir": "./dist",
    "module": "commonjs",
    "target": "es5",
    "lib": ["es6", "dom"],
    "declaration": true,
    "jsx": "react",
    "moduleResolution": "node",
    "experimentalDecorators": true,
    "forceConsistentCasingInFileNames": true,
    "noImplicitReturns": true,
    "noImplicitThis": true,
    "noImplicitAny": true,
    "strictNullChecks": true,
    "suppressImplicitAnyIndexErrors": true,
    "noUnusedLocals": true
  },
  "include": [
    "src/**/*"
  ],
  "exclude": [
    "node_modules",
    "dist" 
  ]
}

Gives the following compiler error:

 error TS4023: Exported variable 'Todo' has or is using name 'IModelType' from external module "C:/dev/me/markd/node_modules/mobx-state-tree/dist/types/complex-types/object" but cannot be named.

Unfortunately it looks like this is not a MST problem and is instead a Typescript issue:

microsoft/TypeScript#5938

Until this is fixed in Typescript or MST it renders MST unusable for my use case in Typescript :(

@mikecann
Copy link
Contributor Author

mikecann commented Oct 6, 2017

If you import it at the top:

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

With my given compiler args you then get:

error TS6133: 'IModelType' is declared but never used.

@mikecann
Copy link
Contributor Author

mikecann commented Oct 6, 2017

Okay so you can get around it if you do this nastyness:

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

export type __IModelType = IModelType<any,any>;

export const Todo = types.model({
    title: types.string
})
.named("Todo")
.actions(self => ({
    setTitle(v: string) {
        self.title = v
    }
}))

export type ITodo = typeof Todo.Type

@mattiamanzati
Copy link
Contributor

Yep, Thats a known typescript behaviour with interfaces :)
Closimg for now

@mikecann
Copy link
Contributor Author

mikecann commented Oct 6, 2017

@mattiamanzati Okay, perhaps put it in the docs, in the Typescript section to make people aware of the issue?

@mattiamanzati
Copy link
Contributor

Yeah, would make sense :)
Are you up to make a PR for that?

@mikecann
Copy link
Contributor Author

mikecann commented Oct 6, 2017

Sure, ill do it now

@mattiamanzati
Copy link
Contributor

Great! Thanks a lot! :)

@mweststrate
Copy link
Member

mweststrate commented Oct 6, 2017 via email

@mikecann
Copy link
Contributor Author

mikecann commented Oct 6, 2017

@mweststrate no, I tried that already, you get the same error

@oadeojo
Copy link

oadeojo commented Jul 3, 2018

Is there a work around for this? I am trying to make my MST store an exportable library but I get

error TS4023: Exported variable 'User' has or is using name 'IModelType'

when I try to compile it.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

No branches or pull requests

4 participants