Given a TS enum
enum Directions { North, South, East, West }
We currently write the decoder as
const directionsDecoder: Decoder<Directions> = oneOf(
constant(Directions.North),
constant(Directions.South),
constant(Directions.East),
constant(Directions.West)
);
which isn't great.
It's worth noting that the most ideal syntax is not possible, since TS compiles out enums and hardcodes the enum values during compilation
const directionsDecoder = enumDecoder(Directions); // this doesn't work!
Something like this should be possible:
const enumDecoder = <E>(...enumValues: Array<E[keyof E]>): Decoder<E> =>
oneOf(...enumValues.map((v) => constant(v)));
const directionsDecoder = enumDecoder(
Directions.North, Directions.South, Directions.East, Directions.West
);
But no matter how I bash at the types I am unable to get the type checker to reconcile the relationship between the elements of the enum and the enum in total.
Given a TS enum
We currently write the decoder as
which isn't great.
It's worth noting that the most ideal syntax is not possible, since TS compiles out enums and hardcodes the enum values during compilation
Something like this should be possible:
But no matter how I bash at the types I am unable to get the type checker to reconcile the relationship between the elements of the enum and the enum in total.