Skip to content

Enumerations

Avoid using native Enums (GraphQL-generated enums are an exception). For more information, read Matt Pocock’s article.

// Option 1 - Union of string literals
type Role = "ADMIN" | "USER" | "GUEST";
const userRole: Role = "ADMIN"; // Valid
// const invalidRole: Role = 'SUPERADMIN';
// Error: Type '"SUPERADMIN"' is not assignable to type 'Role'.
// Option 2 - Object with `as const`
const Roles = {
Admin: "ADMIN",
User: "USER",
Guest: "GUEST",
} as const;
type Role = (typeof Roles)[keyof typeof Roles];
const currentUserRole: Role = Roles.Admin; // Valid
// const invalidRole: Role = 'SUPERADMIN';
// Error: Type '"SUPERADMIN"' is not assignable to type 'Role'.