reasonbucklescriptrescript

How to set a dynamic value as a Js.t key in ReScript?


Is there a way to set a dynamic value as a Js.t key in ReScript?


Solution

  • No it's not. Js.t is a type where all the keys are known at compile time. It's therefore not possible to add or remove keys at runtime. Js.Dict.t is a type that's also represented as a JavaScript object and which allows keys to be added or removed at runtime. The trade-off here is that all the values need to have the same type, encoded in the type parameter given to Js.Dict.t.