简体   繁体   English

如何使用 Node.js 在 JSDoc 中将 typedef 从一个文件“导入”到另一个文件?

[英]How to "import" a typedef from one file to another in JSDoc using Node.js?

Let's say I have a file named "File1.js".假设我有一个名为“File1.js”的文件。 In this file, I export an object of objects and I give each object a typedef, like so.在这个文件中,我导出了一个 object 对象,我给每个 object 一个 typedef,就像这样。

/**
 * My typedef for each object.
 * @typedef {Object} MyObject1
 * @property {String} username Your username
 * @property {String} realname Your real name.
 * @property {boolean} isUnique Are you unique as a person?
 */
module.exports = {
  /**
   * Person One!
   * @type {MyObject1}
   */
  myperson: {
    username: 'TheDragonSlayer',
    realname: 'George',
    isUnique: true
  },
  /**
   * Person Two!
   * @type {MyObject1}
   */
  myperson2: {
    username: 'BobMagee',
    realname: 'Bob',
    isUnique: false
  }
}

Now, in a file named "File2.js", I reference this object in a constructor and set it to a new MyObject1 .现在,在一个名为“File2.js”的文件中,我在构造函数中引用了这个 object 并将其设置为新的MyObject1

const persons = require('./File1.js');

class File2 {
  constructor(options = {}) {
    /**
     * The person for this file.
     * @type {MyObject1}
     */
    this.person = options.person ? persons[options.person] : persons.myperson2;
  }
}

module.exports = File2;

I use Visual Studio Code to develop, so by pressing Ctrl+Space I get IntelliSense.我使用 Visual Studio Code 进行开发,因此通过按 Ctrl+Space 获得 IntelliSense。 Within file one and while I'm making the person objects, IntelliSense tells me that username is a string, realname is a string, and isUnique is a boolean.在文件一中,当我制作人员对象时,IntelliSense 告诉我用户名是字符串,实名是字符串,isUnique 是 boolean。 But, when I go into file2 and reference the newly made person via this.person , when typing this.person.username it does not come up with the expected result of "Username: String".但是,当我将 go 放入 file2 并通过this.person引用新创建的人时,在键入this.person.username时,它不会出现“用户名:字符串”的预期结果。

Is it possible to use the typedef MyObject1 in File2 in vanilla Node.js, or am I out of luck?是否可以在 vanilla Node.js 的 File2 中使用 typedef MyObject1 ,还是我不走运?

Edit: With some more information, I was able to find answers with @export and @import for TypeScript, as well as a tag of sorts that I tried as well.编辑:有了更多信息,我可以使用 @export 和 @import 找到 TypeScript 的答案,以及我尝试过的各种标签。 All of which to no avail.这一切都无济于事。 I also tried marking File1.js as a @module, and doing module:mymodule~MyMethod , but every time I did that it'd just mark this.person as a NodeModule instead of the method itself.我还尝试将 File1.js 标记为 @module,并执行module:mymodule~MyMethod ,但每次我这样做时,它只会将 this.person 标记为 NodeModule 而不是方法本身。

Import the declared type in your file File2.js using the function import . 使用函数import在文件File2.js导入声明的类型。

const persons = require('./File1.js');

/**
 * @typedef {import('./File1.js').MyObject1} MyObject1
 */

class File2 {
...

It works for me. 这个对我有用。

在此输入图像描述 在此输入图像描述

Another thing I discoverd working is to export and empty model and then use it as a reference to the types:我发现的另一件事是导出并清空 model 然后将其用作对类型的引用:

types.js:类型.js:

/** 
* @typedef MyType 
* @prop {string} name  
* @prop {string} description 
*/

export const Types = {}

Then in your other files you can import that and have the types from that dummy object:然后在您的其他文件中,您可以导入该文件并从该虚拟 object 中获取类型:

File1.js文件1.js

import * as Types from "./types.js"

/** @type {Types.MyType} */
const myVar = { name : 'Roy', description : 'abc123'};

pros:优点:

  • alot "cleaner" - less code to write很多“更清洁” - 更少的代码编写
  • only have to import once只需导入一次

cons:缺点:

  • a bit hackey有点哈基
  • not idiomatic不是惯用的
  • you might get the error:你可能会得到错误:

This import is never used as a value and must use 'import type' because 'importsNotUsedAsValues' is set to 'error'此导入从不用作值,必须使用“导入类型”,因为“importsNotUsedAsValues”设置为“错误”

If you have that flag on.如果你有那个标志。

To get around that warning you only need disable it with a ts-ignore:要绕过该警告,您只需使用 ts-ignore 禁用它:

// @ts-ignore  
import * as Types from "./types.js";

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

相关问题 如何将函数从一个文件导出到另一个 node.js? - how to export a function from one file to another node.js? 文档未在 node.js 上定义 - 如何将另一个 .js 文件中的变量导入 node.js 服务器? - Document is not defined on node.js - How can i import a variable from another .js file to node.js server? 如何从 node.js 上的另一个 js 文件导入 object 数组? - How to import object array from another js file on node.js? 将一个文件导入节点js中的另一个文件 - Import one file into another file in node js 从另一个文件导入 JSDOC 文档 - Import JSDOC documentation from another file 如何使用node.js从一个模块到另一个模块访问JSON对象? - How do I access a JSON object from one module to another using node.js? How to import excel file using HTML Input file and read the file content in Node.js (How to send the fullpath to Node.js) - How to import excel file using HTML Input file and read the file contents in Node.js (How to send the fullpath to Node.js) 如何从另一个文件中引用 Node.js 中的变量 - How to refer a variable in Node.js from another file 如何修改Node.js中另一个文件中的变量 - How to modify variables from another file in node.js 无法在另一个文件 (node.js) 中导入函数 - Cannot import a function in another file (node.js)
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM