JSDoc typedef在单独的文件中 [英] JSDoc typedef in a separate file
问题描述
我可以在单独的文件(例如types.jsdoc
)中定义所有自定义类型,以便在整个应用程序中重复使用它们吗?正确的方法是什么?
Can I define all custom types in a separate file (e.g. types.jsdoc
), so that they can be reused throughout the application? What's the right way to do it?
/**
* 2d coordinates.
* @typedef {Object} Coordinates
* @property {Number} x - Coordinate x.
* @property {Number} y - Coordinate y.
*/
推荐答案
您可以在模块中定义类型(例如typedefs.js
).该模块包含您的JSDoc typdef,并且可以简单地导出未使用的属性.
You can define types in a module (eg. typedefs.js
). The module contains your JSDoc typdefs and can simply export an unused property.
// typedefs.js
/**
* @typdef foo
* @property {string} bar
*/
// etc.
exports.unused = {};
要使用它,请将模块导入需要引用以下typdef的位置:
To use it, import the module where you need to reference these typdefs:
const typedefs = require("./typedefs");
/** @type {typedefs.foo} */
const fb = { bar: "hello" };
您可能希望将typedefs.js
注释为@module
或@namespace
.因为我正在使用"tsd-jsdoc"生成一个types.d.ts
文件,并且由于TypeScript现在解释模块与名称空间的方式,所以我已经将我的typedefs.js
文件注释为@namespace
,并将每个typedef记录为该名称空间的成员:
You may wish to annotate typedefs.js
as a @module
or @namespace
. Because I'm using "tsd-jsdoc" to generate a types.d.ts
file, and due to the way TypeScript now interprets modules vs. namespaces, I've annotated my typedefs.js
file as a @namespace
and documented each typedef as a member of that namespace:
/**
* @namespace typedefs
*/
/**
* @typedef foo
* @property {string} bar
* @memberof typdefs
*/
希望有帮助.
这篇关于JSDoc typedef在单独的文件中的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!