📖 Scenario: You are working on a TypeScript project that uses multiple files. You want to tell the compiler about dependencies between files using triple-slash directives.
🎯 Goal: Learn how to add triple-slash directives to reference other TypeScript files so the compiler knows about them.
📋 What You'll Learn
Create a TypeScript file with a triple-slash directive referencing another file
Add a variable to hold the path of the referenced file
Use the triple-slash directive syntax correctly
Print the referenced path to verify
💡 Why This Matters
🌍 Real World
Triple-slash directives help TypeScript projects manage dependencies between files without using module imports.
💼 Career
Understanding triple-slash directives is useful for maintaining legacy TypeScript codebases and for projects that do not use module loaders.
Progress0 / 4 steps
1
Create a TypeScript file with a triple-slash directive
Create a TypeScript file and add a triple-slash directive referencing the file utils.ts using /// <reference path="utils.ts" /> at the top.
Typescript
Hint
Triple-slash directives start with /// and must be at the top of the file.
2
Add a variable for the referenced file path
Add a variable called referencePath and set it to the string "utils.ts".
Typescript
Hint
Use const to create a variable that holds the path string.
3
Use the triple-slash directive syntax correctly
Ensure the triple-slash directive is exactly /// <reference path="utils.ts" /> and placed at the very top of the file before any other code.
Typescript
Hint
Triple-slash directives must be the very first line in the file.
4
Print the referenced path
Write console.log(referencePath) to print the value of referencePath.
Typescript
Hint
Use console.log() to display the variable's value.
Practice
(1/5)
1. What is the main purpose of /// <reference> triple-slash directives in TypeScript?
easy
A. To import external JavaScript libraries at runtime
B. To add comments visible in the final JavaScript output
C. To define variables globally across files
D. To provide compiler instructions about file dependencies and types
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 D