Triple-slash directives help TypeScript find extra files or give special instructions before compiling. They tell the compiler about dependencies or settings.
Triple-slash directives in Typescript
/// <directiveName attribute="value" />Triple-slash directives start with three slashes and are single-line comments.
They must be at the very top of the file before any code.
types.d.ts./// <reference path="./types.d.ts" />node package, useful when working with Node.js./// <reference types="node" />/// <amd-module name="myModule" />This program uses a triple-slash directive to include type definitions from shapes.d.ts. It then defines a function that uses the Circle type and prints a message.
/// <reference path="./shapes.d.ts" /> function drawCircle(radius: number) { const circle: Circle = { radius }; console.log(`Drawing circle with radius ${circle.radius}`); } drawCircle(5);
Triple-slash directives are only for the TypeScript compiler and do not affect runtime JavaScript.
They must be placed at the top of the file before any other statements or imports.
Using /// <reference types="..." /> is common to include types from npm packages.
Triple-slash directives give instructions to the TypeScript compiler using special comments.
They help include type information and manage dependencies between files.
Always place them at the very top of your TypeScript files.
Practice
/// <reference> triple-slash directives in TypeScript?Solution
Step 1: Understand triple-slash directive role
Triple-slash directives are special comments that give instructions to the TypeScript compiler, mainly about type information and file dependencies.Step 2: Identify correct purpose
They do not affect runtime or add global variables but help the compiler understand relationships between files.Final Answer:
To provide compiler instructions about file dependencies and types -> Option DQuick Check:
Triple-slash directives = compiler instructions [OK]
- Thinking they add runtime imports
- Confusing with normal comments
- Assuming they define global variables
Solution
Step 1: Recall triple-slash directive syntax
The correct syntax starts with three slashes followed by a space and then the XML-like tag with attributes.Step 2: Match correct option
/// matches the exact syntax:/// <reference path="..." />. Others miss slashes or use wrong comment style.Final Answer:
/// <reference path="./types.d.ts" /> -> Option AQuick Check:
Triple-slash directive syntax = /// <reference ... /> [OK]
- Using two slashes instead of three
- Using block comments instead of triple-slash
- Omitting the closing slash in the tag
// file: types.d.ts
interface User { name: string; age: number; }
// file: app.ts
/// <reference path="./types.d.ts" />
const user: User = { name: "Alice", age: 30 };
console.log(user.name);What will be the output when compiling and running
app.ts?Solution
Step 1: Understand reference directive effect
The triple-slash directive includes the type definitions fromtypes.d.ts, so theUserinterface is known inapp.ts.Step 2: Check code behavior
The objectusermatches theUserinterface and logsuser.name, which is "Alice".Final Answer:
Alice -> Option BQuick Check:
Reference directive allows type use = "Alice" output [OK]
- Assuming missing import causes error
- Confusing type-only files with runtime code
- Expecting runtime errors from type declarations
/// <reference path="./types.d.ts" />
const user: User = { name: "Bob", age: 25 };
/// <reference path="./extra.d.ts" />
console.log(user.age);What is the problem with this code?
Solution
Step 1: Recall triple-slash directive placement rules
Triple-slash directives must appear at the very top of the file, before any other code or statements.Step 2: Identify violation in code
Here, the second triple-slash directive appears after a variable declaration, which is invalid and causes a compiler error.Final Answer:
Triple-slash directives must be at the very top before any code -> Option AQuick Check:
Triple-slash directives = top of file only [OK]
- Placing directives after code lines
- Thinking multiple directives are disallowed
- Confusing runtime code with compiler instructions
// types.d.ts
interface Config { url: string; timeout: number; }
// settings.d.ts
/// <reference path="./types.d.ts" />
declare const config: Config;
// app.ts
/// <reference path="./settings.d.ts" />
console.log(config.url);What is the correct way to ensure
app.ts compiles without errors and recognizes config and Config types?Solution
Step 1: Understand reference chaining
Sincesettings.d.tsreferencestypes.d.ts,app.tsonly needs to referencesettings.d.tsto access bothConfigandconfig.Step 2: Confirm directive placement
All triple-slash directives must be at the top ofapp.tsbefore any code to work correctly.Final Answer:
Place all triple-slash directives at the top ofapp.tsreferencingsettings.d.tsonly -> Option CQuick Check:
Reference chaining means one directive suffices [OK]
- Adding redundant references
- Placing directives after code
- Re-declaring types unnecessarily
Ready to prove it?
Pass the test and this pattern moves unknown
