How to return record of specific type?

I have a react element that has:

type opt = {label:string, value:string}
type props = { label?: string, value?: string, data: array<opt>}

because rescript figures out the type from the record fields when i want to provide the options it thinks im passing an array of props instead of opts.

i solved it doing:

{
let opt: MyElement.opt = {......}
opt
}

the question is is there a way of returning a record of a specific type without creating a variable and then returning it?

i tried MyElement.opt {…} but it does not work

It’s good practice to put such record types into their own modules, then you can qualify the module where the record comes from after the opening curly brace:

e.g.:

module Opt = {
  type t = {label: string, value: string}
}

type props = {label?: string, value?: string, data: array<Opt.t>}

let make = (props: props) => {
  let opt = {Opt.label: "123", value: "abc"}
  opt
}
1 Like

that makes sense, thanks. Im not used to modules yet

2 Likes