在 JSDoc 中声明 TypeScript 枚举

问题描述 投票:0回答:0

如何使用 JSDoc

@typedef
在 .js 文件中声明 TypeScript
enum
? (这是一段尚无法迁移到 TypeScript 的遗留代码,但需要导入到 TypeScript 模块中。)

请注意,JSDoc 的

@enum
与 TypeScript 的 enum
兼容。

目标是

tsc

 发出以下 
.d.ts
 内容:

declare const enum MyEnum { Apple = "apple", Banana = "banana", Carrot = "carrot", }
失败的尝试

以下方法

不起作用: const MyEnum = /** @type {const} */ { Apple: "apple", Banana: "banana", Carrot: "carrot", }; // TypeScript generates the following `.d.ts`: declare const MyEnum: { Apple: "apple"; Banana: "banana"; Carrot: "carrot"; };

/**
 * @typedef {enum { Apple = 'apple', Banana = 'banana', Carrot = 'carrot' }} MyEnum // ❌ invalid syntax
 */

typescript jsdoc tsc
© www.soinside.com 2019 - 2024. All rights reserved.