1

I would like to know if it is possible to have an array of object contain all keys from a certain enumeration.

An example:

enum Permission {
  CAN_LIST_POST = 'list:post';
  CAN_DELETE_POST = 'delete:post';
}

interface RoleOption {
  title: string;
  value: Permission;
  explanation: string
}

export const RoleOptions = (): RoleOption[] => [
  { title: "List post", value: Permission.CAN_LIST_POST, explanation: "A user can list the posts" },
  // An error should be thrown here because we don't have Permission.CAN_DELETE_POST
]

I have tried using keyof

interface RoleOption {
  title: string;
  value: keyof Permission;
  explanation: string
}

and many others but no lucky. My main idea behind of this is that whenever we have a new permission in the system we only have to add to Permission interface and we will get errors around the system telling where we should add handling of this new permission

Bruno Francisco
  • 3,841
  • 4
  • 31
  • 61

0 Answers0