如何使用 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
*/