Skip to content

Export const arrays alongside union types for better runtime usage #306

@yekver

Description

@yekver

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).

Metadata

Metadata

Assignees

No one assigned

    Labels

    No labels
    No labels

    Projects

    No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions