Sub topic 9
Writing the annotations `isolatedDeclarations` requires
This chapter assumes basic TypeScript types, tsc, tsconfig.json, and .d.ts files. It focuses on the annotations required when isolatedDeclarations is enabled.
What `isolatedDeclarations` checks
A normal declaration emit can inspect several files and use the type checker to determine the public type of an export. For example, TypeScript can follow loadConfig() and discover the return type of config.
With isolatedDeclarations, TypeScript must be able to create a file's declarations from that file alone. This lets other declaration emitters process files independently and potentially in parallel. The trade-off is that exported declarations need enough written type information at their public boundary.
Local variables do not usually need annotations. The important checks apply to exported functions, variables, classes, and other declarations that become part of the generated .d.ts file.
Annotate exported functions
An exported function needs an explicit return type. Parameter types are already required by normal strict TypeScript settings in most codebases, but isolatedDeclarations specifically cares about the complete exported signature.
export function add(a: number, b: number): number {
return a + b;
}
export async function fetchName(id: string): Promise<string> {
return `user-${id}`;
}Without : number, TypeScript would normally infer the return type from the function body. That inference requires examining the body, which violates the isolated declaration rule.
The same rule applies to exported function expressions and arrow functions:
export const multiply = (a: number, b: number): number => a * b;
export const parseId = (value: string): number => Number(value);For callbacks assigned to exported values, annotate the variable as well as the function's parameters when necessary:
export const formatUser: (name: string) => string = (name) => name.trim();Annotate exported variables
An exported variable that gets its type from a function call needs an explicit type. The declaration emitter cannot safely call or inspect that function while working on only the current file.
export interface Config {
retries: number;
}
declare function loadConfig(): Config;
export const config: Config = loadConfig();This rule also applies when the value comes from another module:
import { createClient } from "./client";
import type { Client } from "./client";
export const client: Client = createClient();Use an existing interface or type alias when one describes the value. If no useful name exists, an inline type is still valid:
export const limits: { retries: number; timeoutMs: number } = {
retries: 3,
timeoutMs: 5000,
};Adding an annotation can expose an unintended public type. Treat this as an opportunity to decide what callers should rely on, rather than copying a large inferred type into the export.
Common errors and fixes
| Exported code | Why it fails | Typical fix |
|---|---|---|
| `export function add(a: number, b: number) { ... }` | The return type is inferred from the body | Add `: number` |
| `export const config = loadConfig()` | The variable type comes from a call | Add `: Config` |
| `export const make = () => new Service()` | The exported function's return type is inferred | Add `: Service` |
| `const internal = loadConfig()` | The value is not exported | No annotation required by this rule |
Run the compiler after enabling the option:
{
"compilerOptions": {
"strict": true,
"declaration": true,
"isolatedDeclarations": true,
"outDir": "dist"
},
"include": ["src"]
}Fix diagnostics from the outside inward. Start with exported functions, then exported variables, and finally more complex public declarations such as classes or namespaces. Do not silence the errors with broad types such as any; choose a type that accurately describes the API.
Exercise
Why does export const config = loadConfig() need an annotation when isolatedDeclarations is enabled, while a non-exported local variable usually does not?
Show answer
The exported variable must appear in the generated .d.ts file. An explicit type lets the declaration emitter determine that public type from the current file without checking the implementation of loadConfig.