简体   繁体   English

用 JsDoc 转换 Typescript

[英]Convert Typescript with JsDoc

Can I convert TypeScript file to JavaScript file with JSDoc?我可以使用 JSDoc 将 TypeScript 文件转换为 JavaScript 文件吗?
For example, if I have this main.ts file:例如,如果我有这个main.ts文件:

let x: string = "hello";
// This could be number or array of numbers
let y: number | number[];

It will be converted to something like this main.js file:它将被转换为类似main.js文件的内容:

/**
 * @type {string}
 */
let x = "hello";
/**
 * This could be number or array of numbers
 * @type {number | number[]}
 */
let y;

And so on.等等。
Is there a way to do that?有没有办法做到这一点?
Thank you!谢谢!

While TypeScript doesn't natively provide functionality like this (and for good reason), it is possible to make use of the Compiler API it does provide to interpret types and convert them to JSDoc documentation.虽然 TypeScript 本身不提供这样的功能(并且有充分的理由),但可以使用它提供的编译器 API 来解释类型并将它们转换为 JSDoc 文档。 You can find documentation on the Compiler API in the TypeScript GitHub repository .您可以在TypeScript GitHub 存储库中找到有关编译器 API 的文档。

(Alternatively, you can use the NPM package ts-to-jsdoc to handle this work for you!) (或者,您可以使用 NPM package ts-to-jsdoc为您处理这项工作!)

Disclaimer: I wrote ts-to-jsdoc.免责声明:我写了 ts-to-jsdoc。 You can find the source code at https://github.com/futurGH/ts-to-jsdoc under the MIT license.您可以在 MIT 许可下的https://github.com/futurGH/ts-to-jsdoc找到源代码。

Based on the description of this tool it could be useful for you since it seems to do exactly what you need, ie transpile TypeScript into plain ES code with JSDoc comments - have a look here:根据对该工具的描述,它可能对您有用,因为它似乎完全符合您的需求,即将 TypeScript 转换为带有 JSDoc 注释的纯 ES 代码 - 看看这里:

https://github.com/SoftwareBrothers/better-docs#examples https://github.com/SoftwareBrothers/better-docs#examples

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM