Is there a more concise way to make a function with a parameterized return type?

In typescript, its easy and concise

function jsonParse<T>(str: string): T {
  return JSON.parse(str)
}
let obj = jsonParse<Bookmarks[]>(data)

In rescript this is the best I can come up with, but it seems really verbose, and the aggressive formatting isn’t helping. I’m not that familiar with functors, maybe stuff like this is what they are there for ¯\(ツ)

module MakeJSONParser = (
  M: {
    type t
  },
) => {
  @scope("JSON") external parse: string => M.t = "parse"
  let parse = msg => parse(msg)
}
  module M = MakeJSONParser({
    type t = array<Bookmarks.t>
  })
  let obj = M.parse(msg)

1 Like

You can annotate a let-binding:

external jsonParse: string => 't = "JSON.parse"

type bookmarks = {title: string}

let obj: array<bookmarks> = jsonParse("whatever")

Playground: ReScript Playground

3 Likes