{"id":19167,"library":"babel-plugin-spectypes","title":"babel-plugin-spectypes","description":"Babel plugin (v2.1.11) that compiles spectypes validators at build time, producing fast, eval-free runtime validation for TypeScript/JavaScript. Current stable version is 2.1.11 with bi-monthly releases. Key differentiators: compiles validators into simple if/else statements (no eval or new Function), can outperform ajv in benchmarks, detailed error messages with failure paths, TypeScript type inference, and extensive property-based testing. Requires @babel/core as a peer dependency and spectypes runtime package.","status":"active","version":"2.1.11","language":"javascript","source_language":"en","source_url":"https://github.com/iyegoroff/spectypes","tags":["javascript","babel-plugin","validation","validator","typescript"],"install":[{"cmd":"npm install babel-plugin-spectypes","lang":"bash","label":"npm"},{"cmd":"yarn add babel-plugin-spectypes","lang":"bash","label":"yarn"},{"cmd":"pnpm add babel-plugin-spectypes","lang":"bash","label":"pnpm"}],"dependencies":[{"reason":"peer dependency required for Babel plugin transformation","package":"@babel/core","optional":false},{"reason":"runtime library containing validators and helpers, must be installed separately","package":"spectypes","optional":false}],"imports":[{"note":"Validators are imported from 'spectypes', not the plugin package.","wrong":"import { array } from 'babel-plugin-spectypes'","symbol":"array","correct":"import { array } from 'spectypes'"},{"note":"CommonJS require is supported but ESM import is preferred.","wrong":"const number = require('spectypes').number","symbol":"number","correct":"import { number } from 'spectypes'"},{"note":"Named export, also supports aliases like import { struct as myStruct } from 'spectypes'.","wrong":null,"symbol":"struct","correct":"import { struct } from 'spectypes'"}],"quickstart":{"code":"// Install: npm i spectypes && npm i -D babel-plugin-spectypes\n// .babelrc\n{\n  \"plugins\": [\"babel-plugin-spectypes\"]\n}\n\n// source.ts\nimport { array, number, struct } from 'spectypes'\n\nconst NumberArray = array(number)\n\nconst User = struct({\n  name: string(),\n  age: number()\n})\n\n// The plugin transforms the variable declarations into compiled validators at build time.\n// No runtime compilation needed.","lang":"typescript","description":"Shows installation, Babel configuration, and basic usage with array, number, and struct validators compiled by the plugin."},"warnings":[{"fix":"Review the changelog and update your validator usage accordingly.","message":"Version 2.0.0 introduced major breaking changes; ensure your code is compatible before upgrading from v1.","severity":"breaking","affected_versions":">=2.0.0"},{"fix":"Ensure all validators are imported directly from 'spectypes' and that the Babel plugin is configured correctly.","message":"The plugin only transforms imports from the 'spectypes' package; custom validators or renamed imports require careful setup.","severity":"gotcha","affected_versions":">=1.0.0"},{"fix":"Upgrade to the latest version and replace deprecated validators with their modern equivalents.","message":"Some older validators may be deprecated in future versions; check the documentation for updates.","severity":"deprecated","affected_versions":"<2.0.0"},{"fix":"Ensure babel-plugin-spectypes runs after any import transformation plugins, or use 'plugin ordering' to control execution order.","message":"The plugin runs at build time and may not work correctly if used with other Babel transforms that modify imports before this plugin processes them.","severity":"gotcha","affected_versions":">=1.0.0"},{"fix":"Add '@babel/preset-typescript' to your Babel presets if you are using TypeScript.","message":"TypeScript users must ensure that Babel is configured to handle TypeScript (e.g., @babel/preset-typescript) for the plugin to process .ts files.","severity":"gotcha","affected_versions":">=1.0.0"}],"env_vars":null,"last_verified":"2026-04-25T00:00:00.000Z","next_check":"2026-07-24T00:00:00.000Z","problems":[{"fix":"Run 'npm install spectypes' to install the runtime package.","cause":"Missing runtime dependency 'spectypes'.","error":"Cannot find module 'spectypes'"},{"fix":"Ensure you are using a build step with Babel and Webpack/Rollup, or use CommonJS require correctly: const { array } = require('spectypes').","cause":"Running code directly with Node.js without build step or using CommonJS require incorrectly.","error":"SyntaxError: Unexpected token import"},{"fix":"Add 'import { array } from \\\"spectypes\\\"' at the top of your file.","cause":"Forgetting to import validators from 'spectypes' or import path incorrect.","error":"ReferenceError: array is not defined"},{"fix":"Run 'npm install --save-dev babel-plugin-spectypes' and ensure your .babelrc or babel.config.js includes '\\\"plugins\\\": [\\\"babel-plugin-spectypes\\\"]'.","cause":"The babel-plugin-spectypes package is not installed or not listed in Babel config correctly.","error":"Error: Plugin babel-plugin-spectypes not found"}],"ecosystem":"npm","meta_description":null,"install_score":null,"install_tag":null,"quickstart_score":null,"quickstart_tag":null}