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 defaults to generating bundleless declaration files, which using TypeScript Compiler API and bundle declaration files is also supported by API Extractor.
If you want to generate bundleless declaration files, you can:
dts: true
or dts: { bundle: false }
in the Rslib configuration file.If you want to generate bundle declaration files, you can:
@microsoft/api-extractor
as devDependencies
, which is the underlying tool used for bundling declaration files.dts: { bundle: true }
in the Rslib configuration file.It should be noted that during the generation of declaration files, Rslib will automatically enforce some configuration options in tsconfig.json
to ensure that the TypeScript Compiler API 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. |
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. |