Sub topic 8
What isolatedDeclarations guarantees
This chapter assumes you can read a tsconfig.json, know what a .d.ts file contains, and have run tsc with declaration enabled. It covers what the isolatedDeclarations option promises, what it forbids, and where the guarantee stops. The next chapter covers writing the annotations it asks for.
The problem it solves
Normally, tsc writes a declaration file by running the full type checker. Take this code:
export function add(a: number, b: number) {
return a + b;
}The compiler must work out that the return type is number before it can write export declare function add(a: number, b: number): number;. For this function that is trivial. In general, the inferred type may depend on imports from other files, which depend on further files. To emit one .d.ts file, the compiler may need to check a large part of the program.
That dependency on the whole program has two costs:
- Declaration emit cannot be split across files, because each file's output may depend on the types of other files.
- A tool other than
tsccannot produce declarations unless it reimplements TypeScript's type inference.
isolatedDeclarations, added in TypeScript 5.5, removes this dependency. It is a compiler option that reports an error whenever a file's declaration output could not be produced from that file alone.
The guarantee
When isolatedDeclarations is on and a project compiles without errors, this holds:
> The .d.ts output for any file can be generated by looking only at that file's syntax, without resolving types from other files and without inferring types from expressions.
Put differently, every exported declaration carries enough written type information that a tool can copy it into the .d.ts file. No inference is needed. A tool that only parses the file, with no type checker, can then emit correct declarations.
The option does not change the output. A project that already compiles with isolatedDeclarations produces the same .d.ts files as before. It adds errors, and nothing else.
{
"compilerOptions": {
"composite": true,
"declaration": true,
"isolatedDeclarations": true,
"rootDir": "src",
"outDir": "dist"
},
"include": ["src"]
}The option requires declaration or composite to be enabled. Without one of them, tsc reports a configuration error, because there would be no declaration output to constrain.
What it checks
The rule applies to the exported surface of a file. Local variables, function bodies, and non-exported helpers can still rely on inference. Only the parts that end up in the .d.ts file need explicit types.
| Declaration | Needs an explicit annotation? |
|---|---|
| Exported function parameters | Yes, as always under `strict` |
| Exported function return type | Yes, unless trivially obvious from syntax |
| Exported `const` initialized from a call | Yes |
| Exported `const` initialized with a literal | No, the literal gives the type |
| Exported class property | Yes, unless initialized with a literal |
| Exported class method return type | Yes |
| Non-exported function or variable | No |
| Variables inside a function body | No |
The example below shows both the errors and their fixes:
// Reported as errors when isolatedDeclarations is on:
export function add(a: number, b: number) {
return a + b;
}
export const config = loadConfig();
// Fixed with explicit annotations:
export interface Config {
retries: number;
}
declare function loadConfig(): Config;
export function addFixed(a: number, b: number): number {
return a + b;
}
export const configFixed: Config = loadConfig();Here add fails because its return type must be inferred from a + b. The config constant fails because its type comes from calling loadConfig, whose signature may live in another file. The fixed versions state the types, so a tool can copy them straight into the declaration file.
Why syntax alone is enough for literals
Some inference stays allowed because it needs no type information from elsewhere. The compiler can read the answer off the syntax of the single expression.
export const retries = 3;
export const name = "app";
export const enabled = true;
export const origin = { x: 0, y: 0 } as const;These produce declare const retries = 3; and similar output. A literal's type is visible in its own text. Object and array literals with simple contents are also acceptable when every property can be typed from syntax. A property initialized from an identifier or call is not, because the tool would have to look that identifier up.
Edge cases
Several patterns catch people by surprise.
Inferred return types through spread and computed keys
An exported object literal that uses a spread, such as { ...base, extra: 1 }, needs an annotation. The result type depends on the type of base, which may come from another file. Computed property names have the same problem unless the key is a literal or a well-known symbol.
Default exports
export default of an expression, for example export default someFactory(), needs an annotation, but there is nowhere to put one on an anonymous default export. The usual fix is to name the value first:
interface Settings {
port: number;
}
declare function makeSettings(): Settings;
const settings: Settings = makeSettings();
export default settings;Classes
Class members that appear in the declaration file need explicit types. This includes properties without a literal initializer, accessors, and method return types. Private members are emitted as just private name; with no type, so they do not need annotations.
Re-exports and type-only imports
Re-exporting with export { x } from "./other" is fine, because the declaration file repeats the same re-export and never needs the type of x. This is why barrel files remain cheap under the option.
Declarations that name imported types
An annotation may refer to an imported type, and that is fine. The tool copies the written name and the matching import into the .d.ts file. It does not need to resolve what the name means. The restriction is on inferring types, not on mentioning them.
What it does not guarantee
The option has limits worth knowing before you rely on it.
- It does not check correctness of the whole program. A file with
isolatedDeclarationserrors fixed can still contain type errors in function bodies. Full checking is still needed, and a separatetsc --noEmitrun does it. - It does not make
tscfaster by itself.tscstill does everything it did before. The gain comes from other tools or from--noCheckworkflows that use the guarantee. Later chapters cover both. - It does not guarantee identical output from every tool. The guarantee is that the declarations can be produced locally. Formatting and the ordering of members may differ between
tscand a third-party emitter. - It constrains consumers only through your own exports. Dependencies in
node_modulesare not affected. Their declarations were already written.
How it fits the pipeline
The diagram shows the difference in what declaration emit needs.
When the option is on, the left path is no longer required, which is what lets a separate tool run the right path for many files at once.
Verifying the guarantee
You can see the option work by introducing a violation. Create src/api.ts:
export function double(n: number) {
return n * 2;
}Compile with the config above:
tsc -p .The compiler reports an error on double, saying that the function must have an explicit return type annotation with --isolatedDeclarations. The error code is TS9007. Add : number after the parameter list and the error disappears. Errors in this family are in the 9000 range, such as TS9005 for variables and TS9008 for methods, which makes them easy to search for in build logs.
tsc -p . 2>&1 | grep "TS90"A clean run with no TS90xx lines means the project meets the guarantee.
Trade-offs
The cost is verbosity. Public APIs gain annotations that inference used to supply for free. Many teams find that a benefit for libraries, because the written return type becomes part of the contract and a code change can no longer alter it by accident. Application code that is never consumed as declarations gains little from the option, so it is usually enabled in library packages first.
Exercise
A file contains export const timeout = 5000; and export const client = createClient();, where createClient is imported from another file. With isolatedDeclarations on, which line reports an error, and why?
Show answer
The client line. Its type comes from calling createClient, which would require resolving a type from another file. The timeout line is fine because the literal 5000 gives its type from syntax alone. Fix client by writing its type, for example export const client: Client = createClient();.
Exercise
A project enables isolatedDeclarations, fixes every TS90xx error, and then runs a normal tsc build. Does the emitted .d.ts output change, and is the build faster?
Show answer
The output does not change, because the option only adds errors. The build is not faster on its own, because tsc still parses, binds, checks, and emits as before. The speed gain appears when another tool or a --noCheck workflow emits declarations using the guarantee.