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
You can refer to lib.dts for more details about declaration files configuration.