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

Triple-Slash Directives की जानकारी

Triple-slash directives तीन slash characters से शुरू होने वाले special comments हैं जो TypeScript compiler को instructions देते हैं। वे declaration files, types, libraries, और project configuration reference कर सकते हैं।
Syntax
typescript
/// <reference path="./file.d.ts" />
/// <reference types="package-name" />

Reference Path Directive

Reference path directive TypeScript को बताता है कि current file check करते समय एक और declaration file include करे, जो modern module resolution से पहले dependencies को साथ wire करने का एक early तरीका था।

उदाहरण: Reference Path Directive

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

Reference Types Directive

Reference types directive TypeScript को बताता है कि current file की compilation में एक type package (node_modules/@types से) की declarations include करे, बिना किसी explicit import की ज़रूरत के।

उदाहरण: Reference Types Directive

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

Reference Lib Directive

Reference lib directive explicitly current file की checking में एक built-in TypeScript library declaration include करता है — जैसे एक specific ECMAScript version का APIs — project की tsconfig lib setting पहले से जो include करती है उससे आगे।

उदाहरण: 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

No-default-lib directive एक declaration file को mark करता है कि यह automatically default library include नहीं करे, जो एक custom global environment authoring करते समय उपयोग होता है जिसे standard built-ins inherit नहीं करना चाहिए।

उदाहरण: No-Default-Lib Directive

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

Triple-Slash Directives कब उपयोगी हैं

Modern TypeScript projects आमतौर पर dependency management के लिए triple-slash directives के बजाय imports और tsconfig.json पर rely करते हैं, तो आपको ये ज़्यादातर older code में या hand-written .d.ts files में मिलेंगे।

उदाहरण: 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");
Related Topics
{# common_mistakes/chapter_summary/browser_support: on Hindi pages the view already swaps in the hi_ translation fields (or blanks these out if untranslated), so this renders correctly for both languages without a lang_code check here. #}
आम गलतियां
  1. /// <reference ... /> को दूसरे code या comments-with-code के बाद रखना, जब यह सिर्फ file के top पर valid है।
  2. एक modern project में tsconfig.json के include या एक import के बजाय reference path directive उपयोग करना।
  3. एक types package reference करना जो installed नहीं है, जैसे @types/node के बिना types="node", जो एक error है।
चैप्टर सारांश
  • ES modules code को files में organize करने के लिए import और export उपयोग करते हैं, default exports सहित।
  • Namespaces related code group करते हैं, और declaration merging एक ही नाम वाली कई declarations combine करता है।
  • Ambient declarations और triple-slash directives आपके source के बाहर exist करने वाले code और dependencies describe करते हैं।
🔒

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.