.d.ts की Declaration Files
In this page:
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
// 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
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
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
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
// 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");
- एक
.d.tsfile में real code डालना, जैसेfunction add(a, b) { return a + b; }, जब इसमें सिर्फ declarations हो सकती हैं। - यह उम्मीद करना कि
.d.tsfile runtime function create करेगी, तो इसे call करनाReferenceErrorसे fail होता है। .d.tsfile कोtsconfig.json(include/typeRoots) में include करना भूल जाना, तो compiler इसे ढूंढ नहीं पाता।
Chapter Quiz — Complete all 5 topics to unlock
0/5 topics done
Complete these topics first: