Effect CommunityEC
Effect Community3y ago
26 replies
TheStockBroker

Improving `taggedEnum` with Proxy for a Better API

I had an idea to make taggedEnum better.
If we use Proxy we can get this api:
type State = Data.TaggedEnum<{
  Foo: {...}
  Bar: {}
}>

const State = Data.taggedEnum<State>()

State.Foo({...}) // <-  Proxy catches the access to the `Foo` key, placing it as the key

State.Bar() 

@gcanti @Tim Smart What do you think? It's a better feeling api at the cost of a slight perf hit. This can be overloaded on the regular function object so that it's an opt-in api. I find myself using State("Foo")({...}) pretty frequently, and it's not very good looking
Was this page helpful?