Coding/Learn/typescript/ts literal types enums
TypeScript Professional Developer Complete Course
01.Introduction to TypeScript & The Type Compiler (tsc)
02.Primitive Types: number, string, boolean & bigint
03.Type Inference & Type Widening
04.Top & Bottom Types: any, unknown & never
05.Typed Arrays, ReadonlyArray & Fixed Tuples
06.Literal Types, String Enums & Const Enums
07.Object Types and Shapes
08.Type Aliases
09.Interfaces and Contracts
10.Extending Interfaces
11.Declaration Merging
12.Index Signatures and Record
13.Function Signatures and Return Types
14.Optional, Default, and Rest Parameters
15.Function Overloading
16.Call and Construct Signatures
17.Typing the this Parameter
18.Void vs Undefined vs Never in Functions
19.Union and Intersection Types
20.Type Narrowing with typeof and Truthiness
21.Narrowing with instanceof and the in Operator
22.Discriminated (Tagged) Unions
23.User-Defined Type Guards
24.Assertion Signatures
25.Introduction to Generics
26.Generic Interfaces and Type Aliases
27.Generic Classes and Collections
28.Generic Constraints with extends
29.Type Parameters in Generic Constraints (keyof)
30.Default Type Arguments and Generic Inference
31.Class Fields, Methods, and Constructors
32.Access Modifiers: public, private, protected
33.Parameter Properties Shorthand
34.Static Members and Readonly Modifiers
35.Abstract Classes and Methods
36.Implementing Interfaces with Classes
37.The keyof Type Operator
38.The typeof Type Operator
39.Indexed Access Types
40.Conditional Types
41.The infer Keyword in Conditional Types
42.Mapped Types and Mapping Modifiers
43.Partial, Required, and Readonly
44.Pick and Omit
45.Exclude, Extract, and NonNullable
46.Parameters, ReturnType, and ConstructorParameters
47.Template Literal Types and Key Remapping
48.ES Modules and Type-Only Imports
49.tsconfig.json and Strict Mode
50.Ambient Declarations and .d.ts Files
51.Decorators and Metadata Reflection
52.Building a Type-Safe State Store
53.Comprehensive Knowledge Quiz
54.Capstone: Type-Safe REST Client & ORM Schema
55.Certified Developer Exam
Course Certificate0%

Complete all lessons to earn

LearnLiteral Types, String Enums & Const Enums
6 / 55 0%
Lesson 06 of 55
9 mins read

Literal Types, String Enums & Const Enums

PreviousNext Lesson
In Plain English

Literal types restrict values to exact strings, numbers, or booleans (`"GET" | "POST"`). Enums allow declaring a set of named numeric or string constants.

Deep Dive: How It Works

Literal Unions: `type Method = "GET" | "POST" | "PUT" | "DELETE"`.

String Enums: `enum Direction { Up = "UP", Down = "DOWN" }`.

Const Enums: `const enum` completely inlines values during compilation without generating an object in JS output.

Union vs Enum: In modern TypeScript, string literal unions (`type Status = "idle" | "busy"`) are often preferred over enums.

PointCore Rules to Remember

Point star
Literal Unions Preferred: `"light" | "dark"` is lightweight, zero-runtime, and JSON compatible.
Point star
Const Enums Inline Values: `const enum` inlines constants directly into call sites for performance.

Live Interactive Example

Hit Run Code to see it live
Literal Types and Enums
Python 3.12
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
Output Console
Click "Run Code" to view the rendered output.
How it works: Theme allowed only the three exact string values, and LogLevel provided named constants.

Your Turn: Micro Challenge

No pressure! Edit the starter code below and test your solution with instant feedback.

Micro Exercise

Define Action Literal Union

  • PointDefine `type ActionType = "CREATE" | "UPDATE" | "DELETE";`
  • PointWrite `function handleAction(act: ActionType): string { return "Action executed: " + act; }`
  • PointLog `handleAction("CREATE")`.
1
2
Sandbox Output
Click "Run & Check" to test your solution.

Finished reading and practicing?

Mark this lesson as completed to update your course progress.

PreviousNext Lesson
Overview100 SheetLearnCertificates