Skip to content

Feature request: option to generate const assertions for string enums #431

Open
@CNimmo16

Description

@CNimmo16

Most easily explained with an example, if I have a definition in my schema as follows:

"color": {
  "$id": "#color",
  "type": "string",
  "enum": [
    "blue",
    "red",
    "yellow"
  ]
}

Currently this gets converted into a string union type directly ie. export type Color = "blue" | "red" | "yellow";

I'd like the option to convert these via a const assertion, emitting instead the following:

export const COLORS = ["blue", "red", "yellow"] as const;
export type Color = typeof COLORS[number];

Reason for this probably goes without saying - this would then allow the items to be accessed as values in code.

Could I get your thoughts on inclusion of this, would you accept a PR?

Metadata

Metadata

Assignees

No one assigned

    Projects

    No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions