invariant

Throws an error if the condition is falsy. The error can be specified as the second argument.

Usage

invariant can help to make code easier to read, especially if it replaces nested if/else blocks which all throw errors.

import { invariant } from "@olenbetong/appframe-core";

// before
if (dsMyDataObject.getFields("Name")) {
  if (typeof newValue === "string") {
    dsMyDataObject.currentRow("Name", newValue);
  } else {
    throw Error("Name must be a string");
  }
} else {
  throw Error("Name is not in the data object!");
}

// after
invariant(dsMyDataObject.getFields("Name"), "Name is not in the data object!");
invariant(typeof newValue === "string", "Name must be a string");

dsMyDataObject.currentRow("Name", newValue);

API

Last updated

Was this helpful?