Generate a javascript function that uses a arguments destructuring?

Hello, I have already defined this function which is 99% what I want:

@module external commondir: array<string> => string = "commondir"

let determineDestinationPathKeepingFolderStructure = (~destinationFolder, ~fileFullPath) => {
  let commonPath = commondir([destinationFolder, fileFullPath])
  Node.Path.join2(destinationFolder, fileFullPath->Js.String2.replace(commonPath, "./"))
}

This generates the following function signature:

function determineDestinationPathKeepingFolderStructure(destinationFolder, fileFullPath) {
  var commonPath = Commondir([
        destinationFolder,
        fileFullPath
      ]);
  return Path.join(destinationFolder, fileFullPath.replace(commonPath, "./"));
}

But I want this:

function determineDestinationPathKeepingFolderStructure({ destinationFolder, fileFullPath }) {
// ...
}

How can I generate that so the JS interop is easier for consumers of my function?

ReScript Object is one of ways to achieve your requirement, for example:

let determineDestinationPathKeepingFolderStructure = (
  options: {"destinationFolder": string, "fileFullPath": string},
)  => { ... }

playground

1 Like

Thanks @jihchi , that is close to what I wanted, but it is still not what I wanted. I checked that using a record produces literally the same output and has a nicer rescript syntax:

type options = {destinationFolder: string, fileFullPath: string}

let determineDestinationPathKeepingFolderStructure = (options: options) => {
  let destinationFolder = options.destinationFolder
  let fileFullPath = options.fileFullPath
  let commonPath = commondir([destinationFolder, fileFullPath])
  Node.Path.join2(destinationFolder, fileFullPath->Js.String2.replace(commonPath, "./"))
}

However, my problem is that both outputs are not producing destructuring on the function arguments. This is very important because gives very interesting hints of the expected inputs just with the function signature. Take a look at the difference on the generated intellisense between the RS output:

And the original function at call site:

Yes, is telling you that the fields are any, but at least gives you the field names

Sounds like you want to use genType to generate TypeScript bindings for your code, which will then show up as editor hints for users.

I tried gentype already, and indeed it was generating the expected function signature. However it also adds some unnecessary overhead by wrapping the function on another function and on top of that it also curries and calls the original function. Too much unneeded wrapping for my taste.

I opened an issue on their repo about that