Triple-Slash Directives
A triple-slash directive is a comment of the form /// <reference ... /> at the top of a file. It tells the compiler to include another file, a lib, or a set of types before type-checking the current file.
Detailed Explanation
Before tsconfig.json include lists and import types, TypeScript needed a way for one file to pull in another file's declarations. Triple-slash references are that mechanism. They still exist, and they still matter in declaration files and in a few tooling corners.
The forms you will see:
/// <reference path="./sleep.d.ts" />— include that file in the compilation, as if it were infiles./// <reference types="node" />— include the@types/nodepackage (or a types package of that name) as a global./// <reference lib="dom" />— include a built-in lib, the same namescompilerOptions.libuses./// <amd-module name="..." />— leftover AMD metadata, rare in new code.
path references are how generated .d.ts files keep a graph when a module emits helpers or split declarations. tsc writes them for you if a declaration depends on another emit file that is not imported as a module. You should not hand-write path references in application .ts files; import and tsconfig include are the modern form.
types references are how a script (a file that is not a module) asks for Node or Jest globals without putting "types": ["node"] in tsconfig. In a module, import "node:fs" or a types entry in compilerOptions is clearer. In a .d.ts that must stay a script so it can merge into the global scope, /// <reference types="node" /> is still the right tool.
lib references appear in declaration files that need DOM or ES2020 APIs without forcing every consumer's tsconfig to list that lib. They are a dependency declaration for types, not a polyfill. Including lib="dom" in a server package will make document look legal in that file and can hide accidental DOM usage.
Directives must come before any statement, including imports. A stray blank they can survive; a stray import above them makes the compiler ignore the directive. That is a common "why is this types package not loaded" bug.
Prefer tsconfig lib, types, typeRoots, and references for project-wide setup. Reach for triple-slash when you are authoring a .d.ts that has to pull in a specific lib or @types package for that file only.
Code Examples
1. A declaration file that needs Node globals.
// 1. A declaration file that needs Node globals.
/// <reference types="node" />
export function readConfig(path: string): string {
// Buffer is visible because @types/node was referenced.
const buf: Buffer = Buffer.from("{}");
return buf.toString("utf8");
}2. Pulling a sibling declaration in by path (generated style).
// 2. Pulling a sibling declaration in by path (generated style).
/// <reference path="./user.d.ts" />
export declare function loadUser(id: string): User;3. Asking for the DOM lib from a single file.
// 3. Asking for the DOM lib from a single file.
/// <reference lib="dom" />
export function title(): string {
return document.title;
}Common Gotchas
Directives above the first statement only
an `import` or `export` before the comment means the reference is ignored. Put `///` on line 1.
`lib="dom"` in a server file hides accidental DOM use
prefer listing `DOM` in the client `tsconfig` and keeping the server `lib` to `ES2022` only.
Application code should import, not `reference path`
path references skip module resolution and make files appear in the project twice if they are also `include`d.
`/// <reference types="..." />` is not an npm install
the package still has to exist in `node_modules`. The directive only *includes* it.