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

Const Enums की जानकारी

एक const enum को TypeScript compiler द्वारा inline किए जाने के लिए design किया गया है बजाय एक normal runtime enum object generate करने के। यह generated JavaScript को कम कर सकता है, लेकिन इसके कुछ build-tool और library-distribution considerations हैं।
Syntax
typescript
const enum EnumName {
  Member1,
  Member2,
}
let value = EnumName.Member1;

Basic Const Enum

एक const enum const modifier से declared होता है और normally compile time पर directly इसकी member value में inline हो जाता है बजाय बिल्कुल भी एक runtime enum object generate करने के।

उदाहरण: Basic Const Enum

typescript
const enum Direction {
  Up,
  Down,
}
const d = Direction.Up;
console.log(d);

Const Enum with Functions

Const enum members typed function arguments के रूप में बिल्कुल वैसे ही काम करते हैं जैसे normal enum members, लेकिन इन्हें उपयोग करना compiled output में एक actual runtime enum object बनने से बचाता है।

उदाहरण: Const Enum with Functions

typescript
const enum Color { Red, Green, Blue }
function paint(color: Color) {
  return `Painting with color ${color}`;
}
console.log(paint(Color.Green));

Const Enum and Compilation

Const enums एक compile-time-only feature हैं — इनके members generated JavaScript में directly substitute हो जाते हैं, इसलिए ऐसा कोई object नहीं है जिसे आप runtime पर inspect या iterate कर सकें जैसे एक regular enum के साथ कर सकते हैं।

उदाहरण: Const Enum and Compilation

typescript
const enum Size { Small, Medium, Large }
// At compile time, Size.Medium is inlined directly as the number 1.
console.log(Size.Medium);

Const Enum Limitations

चूंकि const enums inline होने के लिए हैं, code को यह मानकर नहीं चलना चाहिए कि एक runtime enum object exist करता है — यह खासकर तब important हो जाता है जब एक library publish कर रहे हों, क्योंकि कुछ build tools const enums को अलग तरीके से handle करते हैं या इन्हें पूरी तरह disallow करते हैं (isolatedModules)।

उदाहरण: Const Enum Limitations

typescript
const enum Mode { Dev, Prod }
// No runtime object exists to iterate over -- Mode.Dev is just inlined as 0.
console.log(Mode.Dev);

When to Use Const Enums

Const enums internal applications के लिए सूट करते हैं जहां team पूरे toolchain को control करती है और predictable inlining behavior जाना जाता है, final bundle में emitted enum objects की संख्या घटाते हुए।

उदाहरण: When to Use Const Enums

typescript
const enum Env {
  Development,
  Production,
}
console.log(Env.Production);
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. isolatedModules या Babel के साथ एक const enum उपयोग करना, जो errors cause कर सकता है क्योंकि वे files के बीच inline नहीं कर सकते।
  2. Object.keys से एक const enum पर loop करने की कोशिश करना, क्योंकि कोई runtime object exist नहीं करता।
  3. यह उम्मीद करना कि enum compiled JavaScript में दिखेगा, जब इसकी values inline हो जाती हैं।
🔒

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.