1

In Rescript, one can define a Record in this format:

type record1 = {
   a : String
}

but NOT:

type record2 = {
   [a] : String
}

I am looking to write a record that compiles to JS like:

{
   [Op.or]: [12,13]
}

The use case above comes from Sequelize, and the reference is here.

My current solution:

%raw(`{[Op.or]:[12,13]}`)
HellaMad
  • 5,294
  • 6
  • 31
  • 53
heiheihang
  • 82
  • 9
  • 1
    They're called computed properties, and have nothing to do with arrays other than that they share a few lexical tokens. As far as I know the only alternative to using `%raw` is to use [`@set_index`](https://rescript-lang.org/docs/manual/latest/bind-to-js-object#bind-using-special-getter-and-setter-attributes) – glennsl Jun 16 '21 at 18:07
  • Alternatively you can use [`Js.Dict.t`](https://rescript-lang.org/docs/manual/latest/api/js/dict), assuming the keys are strings. – glennsl Jun 16 '21 at 18:09

1 Answers1

2

It's not entirely clear how you intend to interface with the Op construct, whether you can bind to it or not, but here's an example that does, and along with Js.Dict.t effectively produces the same output:

module Op = {
  @val external or: string = "Op.or"
}

Js.Dict.fromList(list{
  (Op.or, [12, 23])
})

It does not directly compile to the JS you want, however, which might be a problem if you rely on something that actually parses the source code. But short of that, I believe this should do what you ask for.

glennsl
  • 28,186
  • 12
  • 57
  • 75