For example, if I have an enum type in TypeScript,
enum Status {
Initialized = 0,
Started = 1,
Done = 2,
Deleted = 4,
}
and I want to write their binding in ReScript. I have seen people done it using a module and variables as following
module Status = {
type t = int
@module("moduleName") @scope("Status")
external initialized: t = "Initialized"
@module("moduleName") @scope("Status")
external started: t = "Started"
@module("moduleName") @scope("Status")
external done_: t = "Done"
@module("moduleName") @scope("Status")
external deleted: t = "Deleted"
}
which seems like a pretty neat solution except for one thing, pattern matching won’t work for these values as they’re just variables. In that case, what’s the best way to resolve this? Like a better type representation, or a better way to handle the type without using pattern matching?
You mean I’ll have to do something like this ? Or is there any other better way to do this?
type t' =
| Initialized
| Started
| Done
| Deleted
let toStatus = (status: t): option<t'> => {
if status === initialized {
Some(Initialized)
} else if status === started {
Some(Started)
} else if status === done_ {
Some(Done)
} else if status === deleted {
Some(Deleted)
} else {
None
}
}