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

String Enums की जानकारी

String enums enum members को explicit string values assign करते हैं। वे logs और APIs में inspect करना अक्सर आसान होता है क्योंकि runtime values meaningful strings होते हैं।
Syntax
typescript
enum EnumName {
  Member1 = "VALUE1",
  Member2 = "VALUE2",
}

Basic String Enum

एक string enum के हर member का एक explicit string value होना चाहिए — numeric enums के विपरीत, TypeScript एक अपने आप assign नहीं करेगा, इसलिए आपको हमेशा exactly पता रहता है कि हर member किस string को represent करता है।

उदाहरण: Basic String Enum

typescript
enum Status {
  Pending = "PENDING",
  Done = "DONE",
  Failed = "FAILED",
}
console.log(Status.Done);

String Enums in Functions

String enum values function parameters के रूप में वैसे ही काम करते हैं जैसे numeric वाले करते हैं, callers को named string choices में से एक तक restrict करते हुए बजाय कोई भी arbitrary string accept करने के।

उदाहरण: String Enums in Functions

typescript
enum Status { Pending = "PENDING", Done = "DONE" }
function label(status: Status) {
  return `Status: ${status}`;
}
console.log(label(Status.Pending));

String Enums and JSON

String enums उन values के लिए खासकर convenient हैं जो JSON से होकर गुज़रती हैं, क्योंकि इनकी runtime value पहले से एक plain, readable string है — enum और इसके serialized form के बीच किसी translation layer की ज़रूरत नहीं।

उदाहरण: String Enums and JSON

typescript
enum Status { Pending = "PENDING", Done = "DONE" }
const task = { id: 1, status: Status.Done };
console.log(JSON.stringify(task));

String Enum Comparisons

String enum members को एक-दूसरे से directly compare किया जा सकता है या switch statements के अंदर cleanly उपयोग किया जा सकता है, और चूंकि values human-readable हैं, debugging output numeric enums से कहीं ज़्यादा आसान पढ़ने में होता है।

उदाहरण: String Enum Comparisons

typescript
enum Status { Pending = "PENDING", Done = "DONE" }
function describe(status: Status) {
  switch (status) {
    case Status.Pending: return "Still working";
    case Status.Done: return "Complete";
  }
}
console.log(describe(Status.Done));

When to Use String Enums

String enums के लिए तब पहुंचें जब values कहीं store, log, या transmit होंगी और human-readable रहने से फ़ायदा होगा — एक numeric enum एक log file या एक REST API payload में sense नहीं बनाता।

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

typescript
enum LogLevel {
  Info = "INFO",
  Warn = "WARN",
  Error = "ERROR",
}
console.log(`[${LogLevel.Warn}] Disk space low`);
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. एक string enum में Pending, जैसी string value छोड़ देना, जो एक string member के बाद एक compile error है।
  2. एक string enum को एक plain string से compare करना, जैसे status === "DONE", जो type-check fail करता है।
  3. Status["DONE"] जैसी एक reverse mapping की उम्मीद करना, जो string enums के पास नहीं होती।
🔒

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.