← Back to TypeScript Course | Chapter 19: Enums Deep Dive | Lesson 4 of 6

Ambient Enums की जानकारी

Ambient enums उन enum declarations को describe करते हैं जो current TypeScript source के बाहर exist करती हैं, आमतौर पर declaration files में। वे TypeScript को enum खुद generate करने के बजाय एक existing runtime API के बारे में बताते हैं।
Syntax
typescript
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

typescript
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

typescript
// 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

typescript
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

typescript
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

typescript
// 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);
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. यह मान लेना कि declare enum runtime पर object बनाता है, इसलिए इसे उपयोग करना ReferenceError के साथ fail होता है।
  2. Ambient enums को same declaration में एक runtime implementation देना।
  3. ऐसी values के साथ members declare करना जो real library की values से match नहीं करती।
🔒

Chapter Quiz — Complete all 6 topics to unlock

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