When working with the code below, it is important that the keys of PropertiesNamesInDataBase
align with those in User.Keys
.
While the values of PropertiesNamesInDataBase
are used in the backend, it is crucial for uniformity that the names match in the frontend.
namespace User {
export enum Keys {
ID = "ID",
name = "name"
}
}
enum PropertiesNamesInDataBase {
ID = "id",
name = "nm"
}
This setup poses at least two challenges:
- We may have to manually retype or copy-paste the keys
PropertiesNamesInDataBase
operates independently fromUser.Keys
, but ideally, the keys inPropertiesNamesInDataBase
should somehow referenceUser.Keys
.
To address the second issue, one solution is to associate the keys in PropertiesNamesInDataBase
with those in User.Keys
:
namespace User {
export enum Keys {
ID = "ID",
name = "name"
}
}
enum PropertiesNamesInDataBase {
[User.Keys.ID] = "id",
[User.Keys.name] = "nm"
}
However, TypeScript does not support this approach:
Computed property names are not allowed in enums. (1164)
If you have any ideas on how to efficiently reuse enum keys or reference PropertiesNamesInDataBase
's keys based on User.Keys
's values, please share your suggestions.