Open oliver3 opened 1 month ago
another option: (least amount of refactor I think)
export const pollingStationStatusList: PollingStationStatus[] = [...];
But probably typescript enum is the most "proper", only reason string literal type is "nicer' is you don't need an extra import / less code.
which would have been useful in a recent PR
@oliver3 Can you please add this issue to the epic of that PR? Because I think the most effective way forward is to take care of it now and use it to improve the code in that PR. An important goal of the zero bug policy is to make a developer's life easier. ;-)
Can you please add this issue to the epic of that PR? Because I think the most effective way forward is to take care of it now and use it to improve the code in that PR. An important goal of the zero bug policy is to make a developer's life easier. ;-)
Eventually we chose a different solution altogether so there is no need anymore in that epic, which was https://github.com/kiesraad/abacus/issues/399.
In the TypeScript community there is some regarding the use of enums, as JavaScript lacks such functionality. Additionally, I found them inconvenient to use due to the necessity of importing the enum type.
One use case I just encountered is to be able to show radio buttons for each value of the PollingStationType.
@praseodym how would you feel about the other suggestions, e.g.
export const pollingStationStatusValues = ["first_entry", "first_entry_in_progress", "definitive"] as const;
export type PollingStationStatus = (typeof pollingStationStatusValues)[number];
or the other way around
export type PollingStationStatus = "first_entry" | "first_entry_in_progress" | "definitive";
export const pollingStationStatusValues: PollingStationStatus[] = ["first_entry", "first_entry_in_progress", "definitive"];
First suggestion looks like the best option to me because the values are not repeated. To me it's a bit more clear without the parenthesis around typeof
, i.e.:
export const pollingStationStatusValues = ["first_entry", "first_entry_in_progress", "definitive"] as const;
export type PollingStationStatus = typeof pollingStationStatusValues[number];
Current implementation
In Abacus, we generate an OpenAPI specification from the Rust types, and an
openapi.ts
file with TypeScript types from that OpenAPI spec.Currently, Rust enum types, such as
are represented in OpenAPI as
and the generated TypeScript type is a union type of string literals
Problem
With this solution, it seems to that there is no way to iterate over the possible values of this type, which would have been useful in a recent PR.
The generation of
openapi.ts
is done ingen_openapi_types.ts
, and could be changed here to generate something else for these enums.Possible other TypeScript types
A quick investigation leads to two other ways to represent this enum in the frontend, whose values can be iterated over:
TypeScript string enum
String array as const, and a type that is generated from its values
Do we see the value of being able to iterate over the values, and which solution do we prefer?