{"id":18868,"library":"ts-pegjs","title":"ts-pegjs","description":"TS PEG.js is a TypeScript code generation plugin for peggy parser generator (v3+). Current stable version is 4.2.1, compatible with peggy ^3.0.2. It generates TypeScript parsers from PEG grammars, supporting custom headers, return type annotations, and various options for type computation. Key differentiators: first-class TypeScript output, supports both CommonJS and ESM formats, and integrates seamlessly with peggy's CLI and API. Previously supported pegjs, now exclusively peggy. Notable breaking changes in v3.0.0 include renaming exported interfaces (dropping 'I' prefix) and changing the default error name from SyntaxError to PeggySyntaxError.","status":"active","version":"4.2.1","language":"javascript","source_language":"en","source_url":"https://github.com/metadevpro/ts-pegjs","tags":["javascript","peggy","pegjs","plugin","ts","typescript","parser","generator"],"install":[{"cmd":"npm install ts-pegjs","lang":"bash","label":"npm"},{"cmd":"yarn add ts-pegjs","lang":"bash","label":"yarn"},{"cmd":"pnpm add ts-pegjs","lang":"bash","label":"pnpm"}],"dependencies":[{"reason":"Required peer dependency for peg parser generation","package":"peggy","optional":false}],"imports":[{"note":"ESM-only since v3; CommonJS require works but not recommended.","wrong":"const tspegjs = require('ts-pegjs');","symbol":"tspegjs (default import)","correct":"import tspegjs from 'ts-pegjs';"},{"note":"For CommonJS environments; the module exports the plugin object directly.","wrong":null,"symbol":"tspegjs plugin object","correct":"const tspegjs = require('ts-pegjs');"},{"note":"Type-only imports are supported; types are available in d.ts files.","wrong":"import { ParseOptions } from 'ts-pegjs';","symbol":"TypeScript types","correct":"import type { ParseOptions, SourceNode } from 'ts-pegjs';"}],"quickstart":{"code":"import peggy from 'peggy';\nimport tspegjs from 'ts-pegjs';\n\nconst grammar = `\n  expr = additive\n  additive = multiplicative (\"+\" multiplicative / \"-\" multiplicative)*\n  multiplicative = primary (\"*\" primary / \"/\" primary)*\n  primary = number / \"(\" expr \")\"\n  number = digits:[0-9]+ { return parseInt(digits.join(''), 10); }\n`;\n\nconst source = peggy.generate(grammar, {\n  output: 'source',\n  format: 'commonjs',\n  plugins: [tspegjs],\n  tspegjs: {\n    customHeader: '// Generated parser',\n    returnTypes: {\n      expr: 'number',\n      additive: 'number',\n      multiplicative: 'number',\n      primary: 'number',\n      number: 'number'\n    }\n  }\n});\n\nconsole.log(source);","lang":"typescript","description":"Generates a TypeScript parser from a simple arithmetic grammar using peggy and ts-pegjs plugin, with return type annotations."},"warnings":[{"fix":"Update code that catches SyntaxError to catch PeggySyntaxError, or set errorName option back to 'SyntaxError'.","message":"In v3.0.0, default errorName changed from SyntaxError to PeggySyntaxError","severity":"breaking","affected_versions":">=3.0.0"},{"fix":"Update import statements to use new interface names: ParseOptions, Cached, TraceEvent, EndExpectation, OtherExpectation.","message":"In v3.0.0, exported interfaces renamed without 'I' prefix (e.g., IParseOptions -> ParseOptions)","severity":"breaking","affected_versions":">=3.0.0"},{"fix":"Migrate from pegjs to peggy and update grammars if needed.","message":"Support for pegjs (v1 and v2) has been removed; only peggy is supported","severity":"deprecated","affected_versions":">=3.0.0"},{"fix":"Either set skipTypeComputation to false (default) or manually provide all return types via returnTypes.","message":"If skipTypeComputation is true, returnTypes option is ignored","severity":"gotcha","affected_versions":">=2.0.0"},{"fix":"If your grammar uses camelCase rule names, set doNotCamelCaseTypes: true to preserve original casing.","message":"CamelCase conversion of type names applies by default; use doNotCamelCaseTypes to disable","severity":"gotcha","affected_versions":">=1.0.0"},{"fix":"Ensure all ts-pegjs options are nested under tspegjs: { ... } in the peggy.generate options object.","message":"Plugin options must be passed under tspegjs key in peggy config object","severity":"gotcha","affected_versions":">=1.0.0"},{"fix":"Install peggy instead of pegjs and update import paths.","message":"Version 2.0.0 dropped support for pegjs and introduced peggy as peer dependency","severity":"breaking","affected_versions":">=2.0.0 <3.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 peggy' or 'yarn add peggy' to install the required peggy parser.","cause":"Missing peer dependency peggy","error":"Error: Cannot find module 'peggy'"},{"fix":"Use: import peggy from 'peggy'; then peggy.generate(...)","cause":"Incorrect import: peggy module does not export generate directly; must use default import or require","error":"TypeError: peggy.generate is not a function"},{"fix":"Ensure tspegjs is imported correctly: const tspegjs = require('ts-pegjs'); or import tspegjs from 'ts-pegjs';","cause":"Passing ts-pegjs incorrectly; plugin must be an object with a use method","error":"Error: Plugin is not a function or does not have a use method"},{"fix":"Use the new names: ParseOptions, Cached, TraceEvent, EndExpectation, OtherExpectation.","cause":"Using v3+ with old interface names that had 'I' prefix","error":"TypeScript error: 'ParseOptions' is not exported from 'ts-pegjs'"},{"fix":"Nest options under a 'tspegjs' property: { tspegjs: { customHeader: '...' } }","cause":"Passing tspegjs options directly instead of under tspegjs key","error":"Error: The 'tspegjs' option must be an object"}],"ecosystem":"npm","meta_description":null,"install_score":null,"install_tag":null,"quickstart_score":null,"quickstart_tag":null}