← Back to index

How to detect the existance of a global value?

Sun May 09 2021

ReScript version: rescript@9.1.2

We can use %external to detect global values.

For example:

type audioContext

let audioContext: option<audioContext> = %external(\"AudioContext")

Which complies to:

var Caml_option = require("rescript/lib/js/caml_option.js");

var audioContext =
  typeof AudioContext === "undefined" ? undefined : AudioContext;

var audioContext$1 =
  audioContext === undefined ? undefined : Caml_option.some(audioContext);

If the variable name starts with _ then we don't need to escape it:

switch %external(__DEV__) {
| Some(_) => Js.log("dev mode")
| None => Js.log("production mode")
}

Which compiles to:

var match = typeof __DEV__ === "undefined" ? undefined : __DEV__;

if (match !== undefined) {
  console.log("dev mode");
} else {
  console.log("production mode");
}

References