Shopify/graphql-tools-web

[graphql-typescript-definitions] - create enum as ts type

davidsonsns opened this issue · 1 comments

Is there any option to generate enum as typescript type?
I mean something like this https://graphql-code-generator.com/docs/plugins/typescript#enumsastypes-boolean-default-value-false.

for example:

enum Status {
    AVAILABLE
    UNAVAILABLE
}

instead of this:

enum Status {
    AVAILABLE: "AVAILABLE",
    UNAVAILABLE: "UNAVAILABLE"
}

could have a param to generate it:

type Status = "AVAILABLE" | "UNAVAILABLE"

Not sure if this answers your question, but this from Typescript doc seems relevant:

The first is that enum members also become types as well! For example, we can say that certain members can only have the value of an enum member:

enum ShapeKind {
    Circle,
    Square,
}

interface Circle {
    kind: ShapeKind.Circle;
    radius: number;
}

interface Square {
    kind: ShapeKind.Square;
    sideLength: number;
}

let c: Circle = {
    kind: ShapeKind.Square, // Error! Type 'ShapeKind.Square' is not assignable to type 'ShapeKind.Circle'.
    radius: 100,
}