SeedCompany / cord-api-v3

Bible translation project management API
MIT License
18 stars 4 forks source link

Fix MadeEnum type to provide value & label from entries implicitly #3225

Closed CarsonF closed 1 month ago

CarsonF commented 1 month ago

Fix type to provide value & label from entries implicitly

Currently:

const Status = makeEnum(...);
Status.entry(...) // => { value: Status, label: string }

BUT if referencing the type generically:

const statusEnum: MadeEnum<Status>;

The entries didn't have a shape:

statusEnum.entry(...) // => empty object

Now they have the implicit shape:

statusEnum.entry(...) // => { value: Status, label: string }

Flip Extra positional generic

This allows declaring extra entry declarations easier, by making it the second arg instead of third.

const statusEnum: MadeEnum<Status, { color: string }>;
statusEnum.entry(...).color // => string

This Extra generic really has nothing to do with the enum functionality, it's just extra "statics". The type could be written as:

MadeEnum<Status> & { <Extras> }

Which we can still do now when needing to reference a generic enum with extra "statics".

I decided to keep the generic though as that helps TS infer how to emit the type.

const X = makeEnum(...); // X is represented as "MadeEnum<...>"

As opposed to some obtuse object literal shape.

https://seed-company-squad.monday.com/boards/5989610236/pulses/6766885746