Preserve Literals

Preserve Literals
πŸ‘¨β€πŸ’Ό We store role permissions in a configuration object. Without as const, TypeScript widens the booleans to boolean, which loses useful information.
🐨 Open
index.ts
and:
  1. Add as const to the roles object so roles.admin.canDeleteUsers stays the literal type true
  2. Create a Role type from the keys of roles
  3. Remove the @ts-expect-error once adminCanDelete: true type-checks
  4. Implement canDeleteUsers(role: Role): boolean using the roles data (admin β†’ true, editor/viewer β†’ false)
  5. Export roles, adminCanDelete, and canDeleteUsers by name

Completion criteria

  • Named exports: roles, adminCanDelete, canDeleteUsers
  • canDeleteUsers('admin') is true; 'editor' and 'viewer' are false
  • With as const, adminCanDelete can be annotated as the literal true
πŸ’° as const preserves true and false as literal types, not just boolean.

Please set the playground first

Loading "Preserve Literals"
Loading "Preserve Literals"
Login to get access to the exclusive discord channel.
Loading Discord Posts