I’m using this code:
type Group = {
[key in ROLES]?: string;
};
enum ROLES {
simple = "simple",
admin = "admin",
}
Is there a way to avoid the "repetition" of simple = "simple"/admin = "admin"?
Can we have simply:
enum ROLES {
simple,
admin,
}
>Solution :
Yes, the enum you declared is valid TypeScript. But instead of strings, this enum contains numbers and is equivalent to:
enum ROLES {
simple = 0,
admin = 1,
}
But you can still extract the key names from ROLES to build the Group type.
type Group = {
[key in `${keyof typeof ROLES}`]?: string;
};