Computer scienceProgramming languagesTypeScriptData TypesThinking of types as sets of values

Type aliases

User roles on a forum

Report a typo

Imagine that you're developing a developer community forum site using TypeScript. The available user roles in this forum are admin, moderator, and user. Each user role should have an array of string values representing their permissions, as follows:

const userPermissions: UserPermissions = {
  admin: ['createCategory', 'updateCategory', 'deleteCategory', 'createPost', 'updatePost', 'deletePost'],
  moderator: ['createPost', 'updatePost', 'deletePost'],
  user: ['createPost', 'readPost', 'commentOnPost', 'upvotePost', 'downvotePost'],
};
  • Define the UserPermissions type alias to represent the possible user roles in the forum. These roles are: 'admin', 'moderator', and 'user' in string format, as indicated by the keys of the userPermissions object.

  • Create the UserPermissions type alias as an object type. It must have properties with keys that match the user roles ('admin', 'moderator', and 'user'), and the values of these properties must be arrays of strings representing the permissions for each role.

Hint

When defining the UserPermissions type, you don't need to explicitly list all the permissions for each role. Instead, just specify that each key (representing a user role) will map to an array of strings (representing the permissions).

Enter a short text
___

Create a free account to access the full topic