Skip to content

Design Philosophy

The core idea of these ReScript bindings is that each interface is modeled as a record type. Where possible, inheritance is represented using record spreading, and methods are modeled as functions in a separate module. This design allows for greater familiarity with the underlying JavaScript APIs, making it more friendly for newcomers.

This project currently supports ReScript 13.0.0-alpha.7. ReScript 13 is still an alpha, and the library may continue to publish 1.0.0-alpha.N releases alongside it. Library alpha releases may include breaking API changes; a stable ReScript compiler is not required to use or release this library’s alpha versions. See the API surface for the public module layout and the release readiness tracker for the current release scope.

The bindings are generated from the MDN Web API documentation. This project aims to reflect a 1:1 mapping of the Web API to the ReScript bindings whenever possible.

In other words, if you are searching for a specific JavaScript binding, begin your journey at the MDN Web API documentation and determine which module contains your sample. Ensure that the module is available in the bindings by checking the specific API. Please open an issue if you require an API that is not yet present.

The bindings are exposed under the WebAPI namespace with the same flat module structure as the original package.

open WebAPI.DOM
let document = WebAPI.DOM.window->WebAPI.Window.document
let myElement: WebAPI.Element.t = document->WebAPI.Document.createElement("div")

Since ReScript does not have the concept of classes or interfaces, the bindings represent the interfaces as record types. These record types can inherit properties from a “base” type through spreading. This can only be done if there are no circular references in the inheritance chain. If circular references exist, spreading is avoided, and the base properties are duplicated instead.

Methods are modeled as functions in a separate module. The idea is that these will be accessible via the -> operator. For better discoverability, tooling enhancements are planned to simplify finding the correct method.

Inherited methods are duplicated in the inheriting module to eliminate the need to cast the type to the base type.

JavaScript supports function overloads, where a function can have multiple signatures. In ReScript, this is not possible, and a method can have multiple bindings with slightly different names. By entering the correct name, tooling should detect all variations of the method.

In some cases, type conversion will be required. Subtypes can safely be cast to their base type using conversion helpers within their module.

open WebAPI.DOM
let document = WebAPI.DOM.window->WebAPI.Window.document
let element: WebAPI.Element.t = document->WebAPI.Document.createElement("div")
let node: WebAPI.Node.t = element->WebAPI.Element.asNode

Converting in the other direction requires proving the runtime type. Broad DOM values commonly come from event libraries, selector APIs, or JavaScript bindings that cannot describe the concrete interface statically. Use the destination module’s classify function for that downcast instead of calling Obj.magic directly.

switch target->WebAPI.HTMLInputElement.classify {
| Some(input) => Some(input.value)
| None => None
}

Classifiers return an option because a runtime value may not implement the requested interface. They also return None when the browser constructor is unavailable. The checked cast remains inside the binding, so application code handles absence explicitly and receives the concrete public interface type only after a successful check.