Interop @new of field from default import

Hello. I need to interop this code:

import FooCore from 'foo/core'

new FooCore.Bar()

You can use ReScript’s default import support: What's new in ReScript 8.3 (Part 1) | ReScript Blog

Combined with the @new decorator. These will produce the output you want. Check the JS output while you write the binding. The Playground is very helpful for the immediate feedback: https://rescript-lang.org/try

I can’t write interop without %raw

The way I suggested should work:

module FooCore = {
  module Bar = {
    type t
    @module("foo/core") @new external new_: unit => t = "Bar"
  }
}

let test = FooCore.Bar.new_()

Of course you will need to set your project config to output ES6 style modules.

However if you can’t get it to work, then %raw is also acceptable.

1 Like

It doesn’t work as I need.

Yeah, I think you’re right. I tried again and managed to get this:

type fooCore
@module("foo/core") external fooCore: fooCore = "default"

type bar
@send external bar: fooCore => bar = "Bar"

let test = fooCore->bar

But this outputs:

import Core from "foo/core";

var test = Core.Bar();

I think we need to be able to use @send and @new together, but the compiler doesn’t allow that. I think you found a legit uncovered use case, unless I am missing something.

1 Like

@snatvb I tried few approaches. This :point_down:t3: worked for me.

module FooCore = {
  type t
  module Bar = {
    type bar
    @module("foo/core") @scope("default") @new external make: unit => bar = "Bar"
  }
}

FooCore.Bar.make()->Js.log
6 Likes

Yep! It works! Thank you.

1 Like

Yes. I think that it would be nice to add to ReScript. It will looks “native”.