Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

docs: bind to a function inside an object with default export #673

Open
wants to merge 1 commit into
base: master
Choose a base branch
from
Open
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
31 changes: 31 additions & 0 deletions pages/docs/manual/latest/bind-to-js-function.mdx
Original file line number Diff line number Diff line change
Expand Up @@ -107,6 +107,37 @@ In a `send`, the object is always the first argument. Actual arguments of the me

Ever used `foo().bar().baz()` chaining ("fluent api") in JS OOP? We can model that in ReScript too, through the [pipe operator](pipe.md).

## Function inside an Object with default export

Binding to a function inside an object with default export. This is only needed if the consumed module is written in ES6.

```js
// math.js
const math = {
add: (x, y) => x + y
}

export default math
```

To create a binding to `add` function you need add the attribute `@scope` with `"default"`.

<CodeTab labels={["ReScript", "JS Output"]}>

```res example
@module("./math.js") @scope("default") external add: (int, int) => int = "add"

let result = add(1, 1)
```

```js
import * as MathJs from "./math.js";

var result = MathJs.default.add(1, 1);
```

</CodeTab>

## Variadic Function Arguments

You might have JS functions that take an arbitrary amount of arguments. ReScript supports modeling those, under the condition that the arbitrary arguments part is homogenous (aka of the same type). If so, add `variadic` to your `external`.
Expand Down