从JavaScript生成TypeScript定义时,如何为枚举成员生成文档



按照从.js文件创建.d.ts文件中的官方说明,我正在尝试为JSDoc@enum生成定义。

/**
* The direction enum.
* 
* @enum {number}
*/
const Direction = {
/**
* Up direction.
* 
* @type {number}
* @constant
*/
Up: 1,
/**
* Down direction.
* 
* @type {number}
* @constant
*/
Down: 2,
/**
* Left direction.
* 
* @type {number}
* @constant
*/
Left: 3,
/**
* Right direction.
* 
* @type {number}
* @constant
*/
Right: 4,
};
export default Direction;

对于我的tsconfig.json,我确保removeComments设置为false。我希望看到Direction对象的所有属性的所有文档,以转移到.d.ts文件,但是,我看到以下输出:

export default Direction;
/**
* The direction enum.
*/
type Direction = number;
declare namespace Direction {
const Up: number;
const Down: number;
const Left: number;
const Right: number;
}

你可以在TS操场上自己试试。我如何才能确保所有文件都能正确保存?

作为一种变通方法,您可以同时使用@typedef@property标记。

不过,我不明白我们为什么要添加@readonly,因为TypeScript常量已经表示了值不能修改的变量,而Direction对象的文字,正如目前定义的那样,它并不是真正的readonly

以下面的定义为例:

/**
* @typedef Direction
* @property {number} Up "Up direction"  
* @property {number} Down "Down direction" 
* @property {number} Left "Left direction" 
* @property {number} Right "Right direction"
*/
const Direction = {
Up: 1,
Down: 2,
Left: 3,
Right: 4,
};
export default Direction;

将生成以下.d.ts文件:

export default Direction;
export type Direction = {
/**
* "Up direction"
*/
Up: number;
/**
* "Down direction"
*/
Down: number;
/**
* "Left direction"
*/
Left: number;
/**
* "Right direction"
*/
Right: number;
};
declare namespace Direction {
const Up: number;
const Down: number;
const Left: number;
const Right: number;
}

这适用于您的用例吗?

干杯

最新更新