This set up means you can own the editor experience of TypeScript-powered editors without porting your project to TypeScript, or having to maintain .d.ts files in your codebase. TypeScript supports most JSDoc tags, you can find the reference here.

To add creation of .d.ts files in your project, you will need to do up-to four steps:

  • Add TypeScript to your dev dependencies
  • Run the TypeScript compiler to generate the corresponding d.ts files for JS files
  • (optional) Edit your package.json to reference the types

TSConfig

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 learn more about the options in the tsconfig reference. An alternative to using a TSConfig file is the CLI, this is the same behavior as a CLI command.

  1. sh
    npx -p typescript tsc src/**/*.js --declaration --allowJs --emitDeclarationOnly --outDir types

TypeScript replicates the node resolution for modules in a package.json, with an additional step for finding .d.ts files. Roughly, the resolution will first check the optional field, then the "main" field, and finally will try in the root.

If absent, then “main” is used

Package.jsonLocation of default .d.ts
No “main” fieldindex.d.ts
“main”:“index.js”index.d.ts
“main”:“./dist/index.js”./dist/index.d.ts