[英]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 而不是方法本身。
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:优点:
cons:缺点:
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.