Loading Depth Study
Preparing your workspace
Union and literal types let TypeScript represent controlled alternatives instead of using overly broad types.
A union type allows a value to be one of multiple types. Literal types restrict a value to specific strings or numbers. These features are especially useful for application states, roles, statuses, and configuration options. Nullable types are useful when data may temporarily be missing, such as an API response that has not arrived yet. TypeScript then requires the code to handle those possibilities safely.
A course status might only be draft, published, or archived. A user role might only be student, mentor, admin, or owner.
type CourseStatus = "draft" | "published" | "archived"; type UserRole = "student" | "mentor" | "admin"; let status: CourseStatus = "published"; let role: UserRole = "student"; console.log(status, role);
The console prints 'published' and 'student'.