Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

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!

like image 520
עתודה אקדמאית Avatar asked Jul 27 '20 22:07

עתודה אקדמאית


1 Answers

While functionality like this is outside of TypeScript's scope, 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.

like image 186
futur Avatar answered Oct 13 '22 01:10

futur