How to generate TypeScript declaration files from JavaScript code?

Sometimes, we want to generate TypeScript declaration files from JavaScript code.

In this article, we’ll look at how to generate TypeScript declaration files from JavaScript code.

How to generate TypeScript declaration files from JavaScript code?

To generate TypeScript declaration files from JavaScript code, we can use the dts-gen module.

To install it, we run

npm install -g dts-gen

Then we install the module that we want to generate types from.

For instance, if we want to generate types for yargs, we run

npm install -g yargs

Then we run

dts-gen -m yargs

to generate the type file for yargs.

We should get the yargs.d.ts file in our project folder.

Conclusion

To generate TypeScript declaration files from JavaScript code, we can use the dts-gen module.