Home > Net >  Should I write one declaration file or one for each class typescript
Should I write one declaration file or one for each class typescript

Time:02-15

I am making a javascript/typescript module and I want it to be javascript and typescript compatible. I hear that you need to use a declaration file for it, but I don't know if you should use just one declaration file or one for each typescript source file.

The compiler by default makes multiple but for all of the modules I have seen so far they all use one.

Please help

CodePudding user response:

If your package is written in TypeScript, you don't need to create declaration files by hand. These will be emitted by the TypeScript compiler when the declaration compiler option is set in your TSConfig.

As you mention, many packages include one main declaration file, specified under the types (or typings) key in package.json. Usually this declaration file is named index.d.ts. A file with this name would be automatically emitted by the compiler for a TypeScript file named index.ts. It's a common practice to re-export everything that your package exports from the index.ts file, and have users of your package import everything from there. This file would be specified under the main, module, or browser keys in package.json, depending on the target environment you're building for. Even if your project consists of many TypeScript files, there will be one JavaScript file and one declaration file as the entry point for your users.

CodePudding user response:

Make your module in typescript and include the build folder while publishing it will automatically support both typescript and javascript as typescript is the superset of javascript

hence

const packageName= require('your_package/build/') or require('yourpackage') is valid for both

  • Related