Object access using variable instead of using Js.Dict

Hi All,
I’m using Object Module from rescript-core dependency. Not using Js.Dict.
let (personName, setPersonName) = React.useState(_ => {Object.empty()})

let nameKey = “name”

At the moment, if I do that, the compiler assumes I’m attempting to access an array and prompts me for an array<'a>.

Getting error as,

This has type: {…“data”: Arr.t}
Somewhere wanted: array<'a>

Thanks in advance🙂

You will not be able to do it with that syntax. For dynamic access of object fields, you will need to use the get or set function of rescript-core. E.g.,

let o = Object.empty()

let key = "yo"
Object.set(o, key, 1)->ignore
Object.get(o, key)->Console.log->ignore

// o[key] <- ERROR!

Not the same question, but somewhat related:


I’m curious why you’re not using reScript records. They are much more type safe than trying to use the Object module in Core.

To add to that the ReScript type system with variants, options, records etc. can model almost anything in a totally type safe and convenient way.