enum-ts-0.1.2 is not a library.
enum-ts
Place this definition somewhere in your project:
/**
* This special type can help generate pattern matchers for you!
* Just use it as so:
* // enum-ts
* type Result<Ok, Err> = Enum<{
* Ok: Ok,
* Err: Err,
* }>
*/
export type Enum<T extends { [Variant: string]: any }> = {
[P in keyof T]: { t: P; c: T[P] };
}[keyof T];
Now whenever you write a type with Enum
definition at the root of a file (cannot be nested in another block), then
enum-ts
will be able to generate exhaustive pattern matchers and constructor code for you!
Install
This is currently just an executable you can install with
Then, you can test it out with one of the examples in tests/
or in this README:
|
Which will print the generated enum matcher helpers!
CLI
# no arguments puts enum-ts into pipe-mode
|
# prints what would be generated from this file (mostly for debugging purposes)
# recursively walks down the directory looking for *.ts & *.tsx files
# to write updates to directly
# "dry-run" will only print out what it would have rewritten the files to if given the `--write` flag.
Examples
Result
Input
type Result<O, E> = Enum<{
Ok: O;
Err: E;
}>;
namespace Result {
export function Ok<O, E>(contents: O): Result<O, E> {
return { t: "Ok", c: contents };
}
export function Err<O, E>(contents: E): Result<O, E> {
return { t: "Err", c: contents };
}
export function apply<O, E, R>(fns: {
Ok(content: O): R;
Err(content: E): R;
}): (value: Result<O, E>) => R {
return function matchResultApply(value) {
// @ts-ignore
return fns[value.t](value.c);
};
}
export function match<O, E, R>(
value: Result<O, E>,
fns: {
Ok(content: O): R;
Err(content: E): R;
}
): R {
return apply(fns)(value);
}
}
Usage
const res = Result.Ok<string, any>("okay value");
Result.match(res, {
Ok(value) {
// do something with value
},
Err(err) {
// do something with err
},
});