Skip to main content

Cheatsheet

The next few sections will cover these in detail. Feel free to skip this one if nothing makes sense yet!

Raw JS​

let add = [%raw "a + b"]
[%%raw "var a = 1"]

String Unicode & Interpolation​

Js.log {js|你好,
世界|js}

let world = "world"
let helloWorld = {j|hello, $world|j}

Global Value​

external setTimeout : (unit -> unit) -> int -> float = "setTimeout" [@@bs.val]

Global Module​

external random: unit -> float = "random" [@@bs.val][@@bs.scope "Math"]
let someNumber = random ()

external length: int = "length" [@@bs.val][@@bs.scope "window", "location", "ancestorOrigins"]

Nullable​

let a = Some 5 (* compiles to 5 *)
let b = None (* compiles to undefined *)

Handling a value that can be undefined and null, by ditching the option type and using Js.Nullable.t:

let jsNull = Js.Nullable.null
let jsUndefined = Js.Nullable.undefined

let result1: string Js.Nullable.t = Js.Nullable.return "hello"
let result2: int Js.Nullable.t = Js.Nullable.fromOption (Some 10)
let result3: int option = Js.Nullable.toOption (Js.Nullable.return 10)

Object​

Records as Objects​

Since BuckleScript 7 (preferred for JS object interop)

type action = {
[@bs.as "type"] type_: string,
username: string
};

[@bs.module "actions/addUser.js"] external createAddUser: string => action = "addUser";

let myAction = createAddUser("reason");

Hash Map Mode​

let myMap = Js.Dict.empty ()
let _ = Js.Dict.set myMap "Allison" 10

Abstract Record Mode​

type person = {
name: string [@bs.optional];
age: int;
mutable job: string;
} [@@bs.deriving abstract]

external getNickname: person -> string = "getNickname" [@@bs.send]

external john : person = "john" [@@bs.val]

let age = john |. ageGet
let () = john |. jobSet "Accountant"
let nick = john |. getNickname

New Instance​

type t
external createDate : unit -> t = "Date" [@@bs.new]

Function​

Object Method & Chaining​

external map : 'a array -> ('a -> 'b) -> 'b array = "map" [@@bs.send]
external filter : 'a array -> ('a -> bool) -> 'a array = "filter" [@@bs.send]

(* 2, 4 *)
let () = [|1; 2; 3|]
|. map (fun a -> a + 1)
|. filter (fun a -> a mod 2 = 0)
|. Js.log

Variadic (was bs.splice prior to version 4.08)​

external join : string array -> string = "join" [@@bs.module "path"] [@@bs.variadic]

Polymorphic Function​

external drawCat: unit -> unit = "draw" [@@bs.module "Drawing"]
external drawDog: giveName:string -> unit = "draw" [@@bs.module "Drawing"]
external padLeft :
string
-> ([ `Str of string
| `Int of int
] [@bs.unwrap])
-> string
= "padLeft" [@@bs.val]

let _ = padLeft "Hello World" (`Int 4)

Curry/Uncurry​

let add = fun [@bs] x y z -> x + y + z
let six = (add 1 2 3) [@bs]

Module​

external dirname: string -> string = "dirname" [@@bs.module "path"]

Import module.exports​

external simpleFunction: string -> string = "simple-module-that-is-a-function" [@@bs.module]

Import Default​

It's important to note that you are not providing the module path to bs.module, but instead as the last argument of external.

external leftPad: string -> int -> string = "./leftPad" [@@bs.module]

Import ES6 default compiled from Babel:

external studentName: string = "default" [@@bs.module "./student"]

Export ES6 default​

let default = "Bob"

Identity External​

Final escape hatch converter. Do not abuse.

external myShadyConversion : foo -> bar = "%identity"

You can find more examples for the various bs.* attributes here.