TypeScript Enum

التعداد في TypeScript (Enum)

Intermediatetypescript1 min read
enumts enumenumerationconst enum

Definition

A TypeScript feature that defines a named set of constant values, giving semantic meaning to fixed option sets.

ميزة TypeScript تُعرِّف مجموعة مسماة من القيم الثابتة، مما يمنح معنى دلاليًا لمجموعات الخيارات الثابتة.

Why It Matters

On 404Fault, status fields like 'published' | 'draft' | 'archived' are string union types rather than enums — they serialize cleanly to Firestore without transformation.

في 404Fault، حقول الحالة مثل 'published' | 'draft' | 'archived' هي أنواع اتحاد سلسلة بدلاً من تعدادات — تُسلسَل بشكل نظيف إلى Firestore دون تحويل.

Full Definition

TypeScript enums create a named set of named constant values. They come in two forms: numeric (default, values are 0, 1, 2...) and string enums (values are strings). String enums are more readable and serializable to Firestore. Modern TypeScript code increasingly prefers string union types over enums because enums compile to runtime code — `type Status = 'active' | 'archived'` has zero runtime cost.
تعدادات TypeScript تُنشئ مجموعة مسماة من القيم الثابتة المسماة. تأتي بشكلين: رقمية (افتراضية) وسلسلة (القيم سلاسل نصية). تُفضَّل التعدادات ذات السلاسل على الرقمية لكونها أكثر قابلية للقراءة والتسلسل إلى Firestore.

Example Usage

// Prefer union types: type ProjectStatus = 'draft' | 'published' | 'archived'; // Over enums: enum ProjectStatus { Draft, Published, Archived }

// أفضّل أنواع الاتحاد: type ProjectStatus = 'draft' | 'published' | 'archived'; // بدلاً من التعدادات

Knowledge Graph

Avoid these mistakes when using TypeScript Enum:

1

Using numeric enums for values stored in a database — they become opaque numbers

2

Const enums disappear after compilation — can cause issues with some bundlers

Sign in to unlock guided AI explanations from AI Teacher.

Generate a Prompt

Copy this prompt and use it directly with any AI model — no setup needed.

Ready-to-Use Prompt
Help me build a project using TypeScript Enum.

Explain:
1. What is TypeScript Enum and why it matters
2. The core architecture and required tools
3. Step-by-step implementation plan
4. Common mistakes to avoid: Using numeric enums for values stored in a database — they become opaque numbers, Const enums disappear after compilation — can cause issues with some bundlers
5. Best practices and production tips

Official Resources