简体   繁体   中英

Convert Typescript with JsDoc

Can I convert TypeScript file to JavaScript file with JSDoc?
For example, if I have this main.ts file:

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:

/**
 * @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. You can find documentation on the Compiler API in the TypeScript GitHub repository .

(Alternatively, you can use the NPM package ts-to-jsdoc to handle this work for you!)

Disclaimer: I wrote ts-to-jsdoc. You can find the source code at https://github.com/futurGH/ts-to-jsdoc under the MIT license.

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:

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

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

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