This chapter introduces what TypeScript Declaration Files are and how to generate declaration files in Rslib.
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:
Bundle declaration files involves bundling multiple TypeScript declaration files into a single declaration file.
Pros:
Cons:
Bundleless declaration files involves generating a separate declaration file for each module in the library, just like tsc
does.
Pros:
Cons:
Rslib supports generating declaration files using both the TypeScript Compiler API and tsgo, and also supports bundling declaration files with API Extractor.
Type | Supported Method | Description |
---|---|---|
bundleless | TypeScript Compiler API | Default method |
bundleless | tsgo | |
bundle | TypeScript Compiler API + API Extractor | Default method |
bundle | tsgo + API Extractor |
Configure in the Rslib config file:
@microsoft/api-extractor
as a development dependency, which is the underlying tool used for bundling declaration files.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.
@typescript/native-preview
as a development dependency:@typescript/native-preview
requires Node.js 20.6.0 or higher.
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:
declarationDir
in tsconfig.json
Configuration item | Description |
---|---|
dts.bundle | Whether to bundle the declaration files. |
dts.distPath | The output directory of declaration files. |
dts.build | Whether to generate declaration files with building the project references. |
dts.abortOnError | Whether to abort the build process when an error occurs during declaration files generation. |
dts.autoExtension | Whether to automatically set the declaration file extension based on the format option. |
dts.alias | The path alias of the declaration files. |
dts.tsgo | Whether to generate declaration files with tsgo. |
banner.dts | Inject content into the top of each declaration output file. |
footer.dts | Inject content into the bottom of each declaration file. |
redirect.dts.path | Whether to automatically redirect the import paths of TypeScript declaration output files. |
redirect.dts.extension | Whether to automatically redirect the file extension to import paths based on the TypeScript declaration output files. |