【问题标题】:How can I use jsDoc with mobx-state-tree?如何将 jsDoc 与 mobx-state-tree 一起使用?
【发布时间】:2019-11-07 15:31:30
【问题描述】:

我正在尝试将 jsDoc 用于我的 mobx-state-tree 驱动的应用程序,但它没有按照我想要的方式做出反应。

Visual Studio 代码没有显示相应类型的正确属性:

jsDoc 在重构和保持 js 代码无错误方面提供了巨大的帮助!但是在这种情况下我该如何使用它呢?

我找到了这个,但似乎没有人真正使用它: https://github.com/Feverqwe/mst-jsdoc-gen

我真的很想知道其他开发者是如何处理这个问题的!

【问题讨论】:

  • 你在使用打字稿吗?
  • 不,我不是。而且我不想迁移我的项目...

标签: visual-studio-code jsdoc mobx-state-tree


【解决方案1】:

问题在于您的 jsDoc 不是指类型的实例,而是指类型本身。

试试这个:

/**
 * @param {typeof Step.Type} step
 */
async applyStep(step){}

或者这个(正如docs 的打字稿部分所指出的那样):

/**
 * @param {Instance<typeof Step>} step
 */
async applyStep(step){}

【讨论】:

    【解决方案2】:

    您的项目可能缺少jsconfig.jsondefines it to be a Javascript project to Visual Studio Code

    例如,给定项目树

    .
    ├── jsconfig.json
    ├── package-lock.json
    ├── package.json
    ├── src
    │   ├── step.js
    │   └── test.js
    └── yarn.lock
    

    jsconfig.json 中定义如下,

    {
        "compilerOptions": {
          "module": "commonjs",
          "target": "es6"
        },
        "include": ["src/**/*"],
        "checkJs": true,
        "allowSyntheticDefaultImports": true
    }
    

    allowSyntheticDefaultImports 配置很重要,因为它允许对项目中的 Javascript 模块中未导出的符号进行代码提示。

    要获得 mobx 状态树模型的类型提示,可以显式记录 typedef。例如,

    import { types } from "mobx-state-tree"
    
    
    const Step = types.model({
        level: types.number,
        strength: types.number
    })
    
     /**
     * @typedef Step
     * @property {Number} level
     * @property {Number} strength
     */
    

    我认为可以更进一步编写一个钩子,为模型动态生成这个typedef 文档。

    我不知道有什么工具可以立即执行此操作。

    您可以分叉您共享的工具,更新它,然后在模型更新时configure a custom task in Visual Studio Code to run it

    【讨论】:

    • 问题不在于代码补全不起作用,问题在于它显示了错误的补全结果(见截图)。这与 mobx-state-tree 非常相关(其他代码工作得很好)
    • 你是对的。我认为您可以获得的最接近的方法是手动显式记录类型定义,或者您共享的工具的更新版本与 vscode 中的自定义任务一起自动化流程。
    猜你喜欢
    • 1970-01-01
    • 2021-09-21
    • 2019-11-26
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2021-04-27
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多