← Back to TypeScript Course | Chapter 9: Modules and Namespaces | Lesson 7 of 7

Triple-Slash Directives

Triple-slash directives are special comments beginning with three slash characters that provide instructions to the TypeScript compiler. They can reference declaration files, types, libraries, and project configuration.

Reference Path Directive

The reference path directive tells TypeScript to include another declaration file when checking the current file, which was an early way of wiring dependencies together before modern module resolution.

Example: Reference Path Directive

typescript
/// <reference path="./other-file.d.ts" />
console.log("References another declaration file for type checking");

Reference Types Directive

The reference types directive tells TypeScript to include declarations from a type package (from node_modules/@types) in the current file's compilation, without needing an explicit import.

Example: Reference Types Directive

typescript
/// <reference types="node" />
console.log("Includes @types/node declarations without an explicit import");

Reference Lib Directive

The reference lib directive explicitly includes a built-in TypeScript library declaration — like a specific ECMAScript version's APIs — into the current file's checking, beyond what the project's tsconfig lib setting already includes.

Example: Reference Lib Directive

typescript
/// <reference lib="es2020.promise" />
console.log("Includes a specific built-in library beyond tsconfig's lib setting");

No-Default-Lib Directive

The no-default-lib directive marks a declaration file as not automatically including the default library, which is used when authoring a custom global environment that shouldn't inherit the standard built-ins.

Example: No-Default-Lib Directive

typescript
/// <reference no-default-lib="true"/>
console.log("Marks this file as not including the default TypeScript library");

When Triple-Slash Directives Are Useful

Modern TypeScript projects usually rely on imports and tsconfig.json for dependency management instead of triple-slash directives, so you'll mostly encounter these in older code or in hand-written .d.ts files.

Example: When Triple-Slash Directives Are Useful

typescript
// Modern projects prefer imports + tsconfig.json:
// import { something } from "./module";
// Triple-slash directives mainly appear in older code or hand-written .d.ts files.
console.log("Triple-slash directives are largely legacy today");
🔒

Chapter Quiz — Complete all 7 topics to unlock

0/7 topics done

Complete these topics first:

Login to run this code

C/C++/Java/PHP execution requires a free account. Your code is saved — you'll land right back in the editor after logging in.