Convert Typescript with JsDoc
Asked Answered
E

2

20

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!

Elata answered 27/7, 2020 at 22:7 Comment(0)
O
13

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.

Oath answered 5/6, 2021 at 2:40 Comment(4)
could you elaborate a bit on the "for good reason"?Kalman
any way to integrate this into npmjs.com/package/gulp-typescript?Stradivarius
Not sure if I did it right but I've taken the code from OP and ran ts-to-jsdoc and no jsdoc can be found in the output. Your repository describes the project as converting "TypeScript code annotated with JSDoc" but OP did not have the code typed with JSDoc already.Milurd
@Milurd You're right — ts-to-jsdoc currently doesn't annotate top-level variables, only functions, classes, interfaces, and types. Feel free to open an issue on the repo if you'd like and I can look into implementing that.Oath
M
0

Based on the description of this tool it could be useful for you since it seems to do exactly what you need, i.e. transpile TypeScript into plain ES code with JSDoc comments - have a look here:

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

Mirk answered 15/2, 2021 at 12:29 Comment(0)

© 2022 - 2024 — McMap. All rights reserved.