Sub topic 10
Rolling out `isolatedDeclarations` in an existing project
This chapter assumes that you know basic TypeScript types, can run tsc, and can edit tsconfig.json. You should also know that a .d.ts file describes a module's public types without containing its runtime implementation.
isolatedDeclarations prepares a project for fast, file-by-file declaration generation. It requires exported declarations to contain enough type information that TypeScript or another tool can generate their declarations without analyzing the entire project.
Why adoption produces errors
Without isolatedDeclarations, TypeScript can infer an exported type by checking other files:
export function createUser(name: string) {
return {
id: crypto.randomUUID(),
name
};
}To write the declaration for this function, the compiler must inspect the function body and infer the returned object. That inference can depend on imports, overloads, and other files.
With isolatedDeclarations, the public type must be stated directly:
export interface User {
id: string;
name: string;
}
export function createUser(name: string): User {
return {
id: crypto.randomUUID(),
name
};
}The annotation is part of the module's public contract. It also makes the code easier to review because readers do not need to inspect the implementation to know what the function returns.
Enable the option gradually
Start in a branch and enable declaration output if the project does not already produce it. TypeScript 5.5 or newer is required.
{
"compilerOptions": {
"strict": true,
"declaration": true,
"isolatedDeclarations": true,
"outDir": "./dist"
},
"include": ["src"]
}Run the compiler without emitting files:
tsc -p tsconfig.json --noEmitThe first run may report errors on exported functions, variables, classes, and re-exports. Fix the errors at module boundaries first. Internal, non-exported code usually does not need new annotations.
Common fixes include:
| Error source | Typical fix |
|---|---|
| Exported function infers its return type | Add a return type |
| Exported variable infers its type from a call | Add a variable type |
| Exported class member lacks a visible type | Annotate the member |
| Re-export hides the public declaration | Add annotations in the original module |
| Exported constant uses a complex expression | Give it an explicit interface or type |
export interface Config {
retries: number;
endpoint: string;
}
declare function loadConfig(): Config;
export const config: Config = loadConfig();
export function retryCount(config: Config): number {
return config.retries;
}A declaration must describe the public result, not expose private implementation details. Prefer a named interface or type when the inferred type would be long or unstable.
Use a staged rollout
Large repositories should not enable the option everywhere at once. Adopt it in these stages:
- Identify packages that emit
.d.tsfiles, especially libraries and shared workspace packages. - Enable
isolatedDeclarationsin one package. - Fix exported API errors and add tests for the package's public types.
- Enable the option for dependent packages.
- Add the option to the shared base configuration after most packages comply.
- Require the compiler check in continuous integration.
A package boundary is a useful rollout unit because its exported declarations already form an API. Avoid weakening the check with broad type assertions merely to silence errors. An assertion can hide a real mismatch between the implementation and the published type.
Check the result before speeding up emit
After the errors are fixed, verify both the normal type check and declaration output:
tsc -p tsconfig.json --noEmit
tsc -p tsconfig.json --emitDeclarationOnlyThe first command checks the program. The second writes .d.ts files without JavaScript output. Once the project has explicit public annotations, a faster declaration pipeline can use --noCheck or an isolated declaration emitter, while a separate full check continues to protect correctness.
The trade-off is annotation work and a stricter public API. In return, declaration generation becomes easier to parallelize and less dependent on the whole source graph.
Exercise
An exported function returns an object whose shape is inferred from its body. What change makes it compatible with isolatedDeclarations?
Show answer
Give the function an explicit return type, preferably a named interface or type that describes the public object shape.