babel-plugin-ts-optchain icon indicating copy to clipboard operation
babel-plugin-ts-optchain copied to clipboard

Babel plugin for transpiling legacy browser support to ts-optchain by removing Proxy usage.

babel-plugin-ts-optchain

Babel plugin for transpiling legacy browser support to ts-optchain by removing Proxy usage.

Input:

import { oc } from "ts-optchain";
oc(data).foo.bar("default");

Output

import { oc } from "babel-plugin-ts-optchain/lib/runtime";
oc(data, ["foo", "bar"], "default");

The babel-plugin-ts-optchain/lib/runtime is a tiny mb like function.

Install

npm install babel-plugin-ts-optchain

and add it to your .babelrc with @babel/preset-typescript

{
  "presets": ["@babel/preset-typescript"],
  "plugins": ["ts-optchain"]
}

Babel Macro

There's also a Babel Macro variant for Create React App and other users who cannot use or don't want to use custom Babel Plugins.

Install the macro with

npm install ts-optchain.macro

Then just in your code import oc from ts-optchain.macro instead of ts-optchain. There's no need to install ts-optchain separately.

import { oc } from "ts-optchain.macro";
oc(data).foo.bar("default");

Limitations

You must call oc() in a single chain. Eg. this does not work:

const x = oc(data);
const bar = x.foo.bar();

Write it like this

const bar = oc(data).foo.bar();