I am trying to convert my JavaScript file to TypeScript definition (.d.ts).
I am unable to find a tool that does this automatically OR a well defined procedure for converting .js to .ts or .d.ts file.
Could anyone please share the proper procedure for getting .d.ts file for .js file?
Thanks,
Saravanan
...and TypeScript will generate *.js and *.d.ts files for us. (Note that the output of the js file is exactly the same we wrote in our js version.) Sadly, as of now tsc does not support generating *.d.ts files from JSDoc annotated files.
Run the TypeScript compiler to generate the corresponding d.ts files for JS files You can learn how to do this in our installation page. The TSConfig is a jsonc file which configures both your compiler flags, and declare where to find files. In this case, you will want a file like the following:
You can however convert your .js file to a TypeScript .ts file and then the typescript compiler can generate a .d.ts for you using the -d compiler flag. e.g. the following will give you a foo.d.ts and a foo.js
this is actually typescripts main use case it gets compiled to .js + d.ts files which you then publish. e.g. even full typescript project have "only" js in their node_modules folder. often these .js files are accompanied by d.ts files or there is a dedicated package @types/packageName.
Unfortunately it is still a manual process. You can however convert your .js
file to a TypeScript .ts
file and then the typescript compiler can generate a .d.ts
for you using the -d
compiler flag. e.g. the following will give you a foo.d.ts
and a foo.js
tsc -d foo.ts --out foo.js
There is disucssion of various tools that you can investigate but none of them are 100% at the moment : https://github.com/borisyankov/DefinitelyTyped/issues/2103#issuecomment-41899391
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With