Declaration files
This chapter introduces what TypeScript Declaration Files are and how to generate declaration files in Rslib.
What is declaration files
TypeScript Declaration Files provide type information for JavaScript code. Declaration files typically have a .d.ts extension. They allow the TypeScript compiler to understand the type structure of JavaScript code, enabling features like:
- Type Checking: Provide type information for JavaScript code, helping developers catch potential type errors at compile time.
- Code Completion: Enhance code editor features like autocomplete and code navigation.
- Documentation Generation: Generate documentation for JavaScript code, providing better developer experience.
- IDE Support: Improve the developer experience in IDEs like Visual Studio Code, WebStorm, and others.
- Library Consumption: Make it easier for users to use and understand your library.
What are bundle declaration files and bundleless declaration files
Bundle declaration files
Bundle declaration files involves bundling multiple TypeScript declaration files into a single declaration file.
-
Pros:
- Simplified Management: Simplifies the management and referencing of type files.
- Easy Distribution: Reduces the number of files users need to handle when using the library.
-
Cons:
- Complex Generation: Generating and maintaining a single bundle file can become complex in large projects.
- Debugging Challenges: Debugging type issues may not be as intuitive as with separate files.
Bundleless declaration files
Bundleless declaration files involves generating a separate declaration file for each module in the library, just like tsc does.
-
Pros:
- Modular: Each module has its own type definitions, making maintenance and debugging easier.
- Flexibility: Suitable for large projects, avoiding the complexity of a single file.
-
Cons:
- Multiple Files: Users may need to handle multiple declaration files when using the library.
- Complex Management: May require additional configuration to correctly reference all files.
How to generate declaration files in Rslib
Rslib supports generating declaration files using both the TypeScript Compiler API and tsgo, and also supports bundling declaration files with API Extractor.
Generate bundleless declaration files
Configure in the Rslib config file:
Generate bundle declaration files
- Install
@microsoft/api-extractoras a development dependency, which is the underlying tool used for bundling declaration files.
- Configure in the Rslib config file:
Generate declaration files with tsgo
This feature is currently an experimental feature. Since tsgo is still in the experimental stage, there may be some bugs and unresolved issues or limitations. So, make sure to fully test it in your project before enabling this option.
- Install
@typescript/native-previewas a development dependency:
@typescript/native-preview requires Node.js 20.6.0 or higher.
- Configure in the Rslib config file:
- In order to ensure the consistency of local development, you need to install the corresponding VS Code Preview Extension and add the following configuration in the VS Code settings:
Notes
During the generation of declaration files, Rslib will automatically enforce some configuration options in tsconfig.json to ensure that the TypeScript Compiler API or tsgo generates only declaration files.
The priority from highest to lowest of final output directory of declaration files:
- The configuration option dts.distPath
- The configuration option
declarationDirintsconfig.json - The configuration option output.distPath or output.distPath.root
