← Back to TypeScript Course | Chapter 11: Type Declarations and DefinitelyTyped | Lesson 1 of 5

.d.ts की Declaration Files

एक .d.ts file में TypeScript declarations होती हैं जो मौजूदा JavaScript code describe करती हैं इसकी runtime implementation प्रदान किए बिना। Declaration files TypeScript को libraries, APIs, और external code समझने में मदद करती हैं।
Syntax
typescript
declare function name(param: type): returnType;
declare const value: type;
export interface Name {}

Declaration File क्या है

एक .d.ts declaration file एक library के types का shape describe करती है — इसके functions, classes, और variables — बिना कोई actual runtime implementation रखे, TypeScript को plain JavaScript में call करने वाले code type-check करने देते हुए।

उदाहरण: What Is a Declaration File

typescript
// math-lib.d.ts (no runtime code, only shapes)
declare function add(a: number, b: number): number;
// The real implementation lives in math-lib.js
console.log("Declaration file describes add() without implementing it");

Ambient Declarations

Ambient declarations (declare keyword उपयोग करते हुए) compiler को बताती हैं कि कुछ runtime पर exist करता है — जैसे एक script tag से injected एक global variable — भले ही TypeScript देख न सके कि यह असल में कहां defined है।

उदाहरण: Ambient Declarations

typescript
declare const APP_VERSION: string;
// Assume a <script> tag defines APP_VERSION globally at runtime.
console.log("Ambient declaration for a global injected elsewhere");

Classes Declare करना

एक .d.ts file में एक class declare करना इसके constructor signature, public members, और उनके types list करता है तो consumers को autocomplete और type-checking मिलती है एक class के against जिसका real implementation एक separate .js file में रहता है।

उदाहरण: Declaring Classes

typescript
declare class EventEmitter {
  on(event: string, handler: Function): void;
  emit(event: string): void;
}
// Real implementation lives in a separate .js file.
console.log("Class shape declared for a JS implementation");

Functions Declare करना

एक .d.ts file में एक function का signature declare करना — इसके parameter types और return type — compiler के लिए उस function की actual logic की एक line भी देखे बिना call sites पर misuse पकड़ने के लिए काफी है।

उदाहरण: Declaring Functions

typescript
declare function parseCsv(input: string): string[][];
// Only the signature is known here, not the logic.
console.log("Function signature declared, catches misuse at call sites");

Declaration Files कब उपयोग होती हैं

Declaration files आमतौर पर उपयोग होती हैं जब एक JavaScript library TypeScript consumers को ship की जा रही हो, जब एक global script के लिए types लिखी जा रही हों जो module नहीं है, या जब DefinitelyTyped project ने अभी किसी package के लिए types publish न की हों।

उदाहरण: When Declaration Files Are Used

typescript
// Used when: shipping a JS library, typing a global script,
// or DefinitelyTyped hasn't published types yet.
declare function legacyWidget(id: string): void;
console.log("Declaration files bridge JS libraries into TypeScript");
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. एक .d.ts file में real code डालना, जैसे function add(a, b) { return a + b; }, जब इसमें सिर्फ declarations हो सकती हैं।
  2. यह उम्मीद करना कि .d.ts file runtime function create करेगी, तो इसे call करना ReferenceError से fail होता है।
  3. .d.ts file को tsconfig.json (include / typeRoots) में include करना भूल जाना, तो compiler इसे ढूंढ नहीं पाता।
🔒

Chapter Quiz — Complete all 5 topics to unlock

0/5 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.