-
-
Notifications
You must be signed in to change notification settings - Fork 137
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Generate readonly arrays and corresponding union types for enum types #864
Comments
This is very useful. Is there a reason to use this over Typescript enums, which combine the functionality of the runtime variables and the compile-time types? |
The Typescript string enum output implementation would look like this: outputFile.addEnum({
name: name + '_enum',
members: values.map((value) => ({
name: value,
value,
})),
isExported: true,
}); To produce output like this: export enum product_approval_status_enum {
"action-needed" = "action-needed",
disputed = "disputed",
pending = "pending",
approved = "approved"
} You can use the enum as a type: let status: product_approval_status_enum | null = null;
// ...
status = product_approval_status_enum.approved; Or you can use const approval_statuses = Object.values(product_approval_status_enum);
//...
const type_checked_value: product_approval_status_enum = approval_statuses[0]; |
@toBeOfUse , The downside to Typescript Enums is that they cannot be defined inline, which prevents constructing a hierarchical structure. Additionally, postgresql enum values can have characters that aren't valid for Typescript enum keys. |
@kamilogorek , how about #901 ? |
Currently our enums are generated like so:
This however makes it impossible to be used in all kinds of parsers or validators, which require a value as an input, not the type, as those are stripped during compilation.
A solution to this is ti generate a type for every corresponding enum in a form of:
and somehow expose it publicly.
Our existing `ts-morph` implementation below (click to expand)
The text was updated successfully, but these errors were encountered: