Sub topic 8
What isolatedDeclarations guarantees
Assume you know basic TypeScript types, can run tsc, and understand that a .d.ts file describes a module's public types without containing its implementation. This chapter focuses on what isolatedDeclarations guarantees and what it does not.
The problem declaration emit solves
When TypeScript emits JavaScript, it can usually process each source file separately. Declaration emit is different. To write an accurate .d.ts file, TypeScript may need to infer the type of an exported value from its implementation.
export function add(a: number, b: number) {
return a + b;
}
export const settings = createSettings();
function createSettings() {
return { retries: 3 };
}The declaration output depends on examining the function body and following the type of createSettings:
export declare function add(a: number, b: number): number;
export declare const settings: {
retries: number;
};That inference can require information from other declarations or files. A tool that wants to generate declarations in parallel cannot safely process this file using only its own source and already available type declarations.
The guarantee from isolatedDeclarations
isolatedDeclarations requires exported declarations to contain enough local type information for a declaration emitter to process one file independently.
Enable it with declaration output:
{
"compilerOptions": {
"strict": true,
"declaration": true,
"isolatedDeclarations": true,
"outDir": "./dist"
},
"include": ["src"]
}With this option enabled, TypeScript reports errors when an exported declaration depends on implementation inference that cannot be derived from the file alone. Additions such as explicit return types and variable annotations make the public contract local and clear.
export interface Settings {
retries: number;
}
declare function createSettings(): Settings;
export function add(a: number, b: number): number {
return a + b;
}
export const settings: Settings = createSettings();A declaration emitter can now read the exported signatures without evaluating the function bodies.
What it does not guarantee
isolatedDeclarations does not mean that the program is type-safe. Run a normal type-checking pass such as tsc --noEmit to catch incompatible assignments, invalid calls, and other semantic errors.
It also does not automatically make the TypeScript compiler faster. The main benefit is that independent tools can emit declarations concurrently. TypeScript can use the restrictions as a contract, while a build system chooses how to parallelize the work.
The option does not require every local variable to have an annotation. It primarily affects declarations that cross the module boundary, including exported functions, classes, variables, and types.
Common errors and fixes
The exact diagnostics depend on the declaration, but these patterns commonly need annotations:
- Exported functions need explicit return types.
- Exported variables initialized from calls or expressions need explicit types.
- Public class members may need explicit types.
- Exported constants whose literal type matters may need an intentional annotation.
Keep annotations at the public boundary. Do not add types to every temporary variable unless they improve readability or fix a specific diagnostic.
Adopting the option
Enable isolatedDeclarations in a branch and run the declaration build. Fix errors from exported APIs first. Then run both checks:
tsc --noEmit
tsc --declaration --emitDeclarationOnlyThe first command checks the program. The second verifies that declaration output can be generated under the new restrictions. In a larger repository, adopt the option package by package and keep the generated .d.ts files under review.
Exercise
Does isolatedDeclarations replace the normal TypeScript type checker?
Show answer
No. It constrains public declarations so they can be emitted independently. Run a separate type-checking pass to validate the program's semantics.