question-mark
Stuck on an issue?

Lightrun Answers was designed to reduce the constant googling that comes with debugging 3rd party libraries. It collects links to all the places you might be looking at while hunting down a tough bug.

And, if you’re still stuck at the end, we’re happy to hop on a call to see how we can help out.

Generate object literals for enums

See original GitHub issue

We’re currently rolling out this pattern.

export * from '@genql/client'
import type { PlanetscaleRegion as PlanetscaleRegion_ } from '@genql/client'

export type PlanetscaleRegion = PlanetscaleRegion_

// TODO get object literals for enums from Genql core.
export const PlanetscaleRegion = {
  us_east: 'us_east',
  us_west: 'us_west',
  ap_northeast: 'ap_northeast',
  ap_south: 'ap_south',
  ap_southeast: 'ap_southeast',
  eu_west: 'eu_west',
} as const

This is so that frontend code can create enum members and more (such as create zod schema types based on the enum for frontend form validation).

It would be great to have Genql ship (generate) these object literals by default.

Issue Analytics

  • State:closed
  • Created 2 years ago
  • Comments:7 (3 by maintainers)

github_iconTop GitHub Comments

3reactions
remorsescommented, Nov 1, 2021

You can now import enumPlanetscaleRegion and do enumPlanetscaleRegion.us_west

0reactions
jasonkuhrtcommented, Nov 1, 2021

If you can allow a way to customize the naming pattern in the CLI/config file that would be ideal 😃

Customize examples:

${EnumName}           // Our personal preference
staticText${EnumName}
static_text_${snakeCase(EnumName)}

I guess anything requiring transformations would be relegated to a config file, not on the CLI.

Read more comments on GitHub >

github_iconTop Results From Across the Web

Object literals combining enums and computed property names
I have an enum enum Action { action1 = 'action1', action2 = 'action2' };. whose values I use as computed property names in...
Read more >
Use const enum values as keys of object literals · Issue #14682
I want to use values of a const enum to define object literals ** Syntactic ** const enum E { Aa = 1,...
Read more >
Creating Enums in Vanilla JavaScript | Bits and Pieces
JavaScript enumeration: a basic representation · Step 1. Define a plain object literal (POJO) · Step 2. Make sure that values are constant....
Read more >
13 Alternatives to enums in TypeScript
13 Alternatives to enums in TypeScript · 13.1 Unions of singleton values # · 13.2 Discriminated unions # · 13.3 Object literals as...
Read more >
Handbook - Enums - TypeScript
In this generated code, an enum is compiled into an object that stores both forward ( name -> value ) and reverse (...
Read more >

github_iconTop Related Medium Post

No results found

github_iconTop Related StackOverflow Question

No results found

github_iconTroubleshoot Live Code

Lightrun enables developers to add logs, metrics and snapshots to live code - no restarts or redeploys required.
Start Free

github_iconTop Related Reddit Thread

No results found

github_iconTop Related Hackernoon Post

No results found

github_iconTop Related Tweet

No results found

github_iconTop Related Dev.to Post

No results found

github_iconTop Related Hashnode Post

No results found