Currently, when using the types option (default one) only string unions are generated. While this is ideal for keeping the codebase flexible and easy to integrate with existing string-based logic, it introduces a limitation: you can’t iterate over the possible values at runtime.
Proposal
For every string union, also generate a const array of its values and derive the type from it.
Instead of:
export type Colors = 'red' | 'green' | 'blue'
Produce this:
export const Colors = ['red', 'green', 'blue'] as const;
export type Colors = typeof Colors[number];
Why not enums?
While enums seem a clean solution at first glance, they introduce friction in large codebases:
- If a codebase already uses plain string, switching to enums means a full refactor.
- Comparing similar values across multiple schemas becomes fragile (eg.: two identical enums exported by different schemas cannot be directly compared (
EnumA.ACTIVE !== EnumB.ACTIVE).
Currently, when using the types option (default one) only string unions are generated. While this is ideal for keeping the codebase flexible and easy to integrate with existing string-based logic, it introduces a limitation: you can’t iterate over the possible values at runtime.
Proposal
For every string union, also generate a const array of its values and derive the type from it.
Instead of:
Produce this:
Why not enums?
While enums seem a clean solution at first glance, they introduce friction in large codebases:
EnumA.ACTIVE !== EnumB.ACTIVE).