Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Procedure for generating .d.ts from js file

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

like image 927
user3309953 Avatar asked Apr 30 '14 03:04

user3309953


People also ask

Can typescript generate DTS files from JSdoc annotated files?

...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.

How do I generate DTS files for JS 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:

How do I convert a JS file to a typescript file?

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

How does typescript get compiled?

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.


1 Answers

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

like image 191
basarat Avatar answered Sep 23 '22 10:09

basarat