The `jsdoctypeparser` library provides a strict parser for various JavaScript type expression syntaxes, including JSDoc, Closure Compiler, and a subset of TypeScript types. It is currently at version 9.0.0. The library transforms these type expressions into a detailed Abstract Syntax Tree (AST), enabling programmatic analysis and manipulation. It also offers a `publish` function to stringify an AST back into a type expression, with capabilities for custom stringification via a publisher API. Additionally, a `traverse` function allows for AST navigation with `onEnter` and `onLeave` handlers. This tool is crucial for projects needing to deeply understand, validate, or transform JSDoc-style type annotations, serving as a foundational component for linters, documentation generators, and code transformation tools. Its differentiators lie in its strict parsing capabilities across multiple dialects and its comprehensive AST manipulation utilities, which allow for a high degree of control over type expression processing. The project maintains an active development status, focusing on robustness and specification adherence.
npm install jsdoctypeparserVerified import paths — ran on the pinned version, not inferred.
This quickstart demonstrates how to parse a JSDoc type expression into an AST, traverse the generated AST, and then stringify it back using `parse`, `traverse`, and `publish` functions.
Consult the project's changelog or release notes for specific AST changes in each major version, and adapt AST processing logic accordingly. Be prepared for adjustments when upgrading.
Always test with your specific type expressions. For ambiguous or complex types, simplify them if possible or consult the library's source/tests to understand its interpretation. The [live demo](https://jsdoctypeparser.github.io) is an excellent tool to quickly test expressions and view the resulting AST.
When creating custom publishers, ensure comprehensive coverage for all `NodeType`s (inspect `lib/NodeType.js`). For AST modification and subsequent publishing, validate published output against expected string representations, especially after manual changes to the AST structure.
Integrate with a separate type-checking solution (e.g., TypeScript, Closure Compiler, or custom linters) for semantic validation. Use `jsdoctypeparser` for understanding the *structure* of type annotations and enabling transformations, not for their runtime or compile-time validity.
Install the package using your preferred package manager: `npm install jsdoctypeparser` or `yarn add jsdoctypeparser`. Double-check the module name in your code.
Ensure you are using the correct CommonJS destructuring: `const { parse } = require('jsdoctypeparser');`. If you are in an ESM context, you might need `import * as JSDocParser from 'jsdoctypeparser'; const { parse } = JSDocParser;` or ensure your build tools (e.g., Webpack, Rollup) are configured for CommonJS interop.Review your type expression for grammatical correctness based on JSDoc, Closure Compiler, or the supported TypeScript type subset. Test the problematic expression directly in the [jsdoctypeparser live demo](https://jsdoctypeparser.github.io) to quickly identify the exact parsing error and expected AST.
No dependency data recorded yet.