babel-preset-jsdoc-to-assert

raw JSON →
5.0.0 verified Sat Apr 25 auth: no javascript

Babel preset that converts JSDoc type annotations into runtime assertion statements using babel-plugin-jsdoc-to-assert. Version 5.0.0 is current and requires Babel 7+. It is a thin wrapper around the core plugin, intended for easy integration via Babel presets. Release cadence is low; this package sees infrequent updates. Key differentiator: simplifies adding JSDoc-based runtime type assertions without manual checks.

error Error: Plugin/Preset files are not allowed to export objects, only functions.
cause Using the preset as a require()d object instead of a string in Babel config.
fix
Set presets: ['jsdoc-to-assert'] in .babelrc or babel.config.js.
error Module not found: Can't resolve 'babel-preset-jsdoc-to-assert'
cause Package not installed or incorrect Babel version (Babel 6 vs 7).
fix
Run npm install --save-dev babel-preset-jsdoc-to-assert and ensure Babel 7 is used.
breaking v5.0.0 requires Babel 7.x; presets from v4.x are incompatible with Babel 7.
fix Upgrade to Babel 7 and use preset 'jsdoc-to-assert'.
breaking v4.0.0 updated babel-plugin-jsdoc-to-assert to 3.0.0 which may change assertion output.
fix Review generated assertions; see plugin changelog for details.
gotcha Preset only includes a single plugin; it does not configure any other Babel transformations.
fix Combine with other presets (e.g., @babel/preset-env) for full compilation.
gotcha Preset name must be 'jsdoc-to-assert' in config; using full package name 'babel-preset-jsdoc-to-assert' may fail.
fix Use 'jsdoc-to-assert' in presets array.
npm install babel-preset-jsdoc-to-assert
yarn add babel-preset-jsdoc-to-assert
pnpm add babel-preset-jsdoc-to-assert

Install preset and transform JSDoc type annotations to runtime assertions via Babel CLI.

// Install
npm install --save-dev babel-preset-jsdoc-to-assert @babel/core @babel/cli

// .babelrc
{
  "presets": ["jsdoc-to-assert"]
}

// Input: src/example.js
/** @param {number} n */
function double(n) {
  return n * 2;
}

// Run: npx babel src --out-dir lib
// Output: lib/example.js
function double(n) {
  if (!(typeof n === 'number')) throw new Error("assertion failed: n is not a number");
  return n * 2;
}