Ambient Enums की जानकारी
In this page:
declare enum EnumName {
Member1,
Member2,
}
Declaring an Ambient Enum
declare enum syntax बिना current TypeScript file में कोई runtime implementation दिए एक enum का shape describe करता है — आप compiler को बता रहे हैं 'यह कहीं और exist करता है, मेरा भरोसा करो।'
उदाहरण: Declaring an Ambient Enum
declare enum NativeColor {
Red,
Green,
Blue,
}
// Implementation assumed to exist elsewhere at runtime.
console.log("Ambient enum NativeColor declared");
Ambient Enums in Declaration Files
Ambient enums आमतौर पर .d.ts declaration files के अंदर enum-like values describe करने के लिए रखे जाते हैं जो actually किसी दूसरी JavaScript library या खुद runtime environment द्वारा supply की जाती हैं।
उदाहरण: Ambient Enums in Declaration Files
// colors.d.ts
// declare enum NativeColor { Red, Green, Blue }
console.log("Ambient enums commonly live in .d.ts files");
Ambient Enum Members
Ambient enum declarations explicit initializers include कर सकती हैं या members को uninitialized छोड़ सकती हैं, लेकिन दोनों ही तरह से इनका real meaning पूरी तरह उस external implementation पर depend करता है जो runtime पर इन्हें actually backs करती है।
उदाहरण: Ambient Enum Members
declare enum HttpMethod {
GET = "GET",
POST = "POST",
}
console.log("Members can have explicit or implicit values, backed externally");
Ambient Enums and JavaScript APIs
Ambient declarations सही tool हैं जब एक JavaScript API genuinely runtime पर exist करती है लेकिन अपनी खुद की TypeScript type definitions के बिना ship होती है — आप TypeScript में missing type information भर रहे हैं।
उदाहरण: Ambient Enums and JavaScript APIs
declare enum ReadyState {
Loading,
Complete,
}
// Describes a JS API's real runtime enum-like values.
console.log("Ambient declarations fill in types for a real external API");
When to Use Ambient Enums
एक ambient enum के लिए specifically तब पहुंचें जब कहीं और implement की गई किसी चीज़ को describe कर रहे हों — अपने खुद के application के लिए एक brand-new enum बनाने के तरीके के रूप में नहीं, जो सिर्फ एक normal enum declaration होना चाहिए।
उदाहरण: When to Use Ambient Enums
// Use declare enum only for something implemented elsewhere.
// For your own app's enum, just write a normal enum:
enum AppStatus { Active, Inactive }
console.log(AppStatus.Active);
- यह मान लेना कि
declare enumruntime पर object बनाता है, इसलिए इसे उपयोग करनाReferenceErrorके साथ fail होता है। - Ambient enums को same declaration में एक runtime implementation देना।
- ऐसी values के साथ members declare करना जो real library की values से match नहीं करती।
Chapter Quiz — Complete all 6 topics to unlock
0/6 topics done
Complete these topics first: