acorn-typescript is an experimental alternative plugin for Acorn, a small, fast JavaScript parser. It extends Acorn to parse TypeScript scripts into a TypeScript AST, aiming for faster performance compared to other TypeScript parsers. The current stable version is 1.4.13, and it exhibits an active release cadence, frequently addressing bug fixes related to various TypeScript syntax features like optional class properties, generic types, arrow functions, and decorators. Key differentiators include its integration as an Acorn plugin, allowing existing Acorn users to add TypeScript parsing capabilities, and its specific focus on optimizing parsing speed. It supports standard TypeScript syntax, decorators, and JSX/TSX. It requires `acorn` version `>=8.9.0` as a peer dependency.
npm install acorn-typescriptVerified import paths — ran on the pinned version, not inferred.
Demonstrates how to extend Acorn's parser with the `acorn-typescript` plugin to parse TypeScript code, including basic syntax and ambient contexts using the `dts` option, ensuring `locations: true` is set.
Always include `{ locations: true }` in the options object passed to `acorn.parse()` or `acorn.Parser.extend(...).parse()`.Ensure `locations: true` is always provided in the parser options. Refer to the `quickstart` example for correct usage.
Install a compatible version of `acorn` alongside `acorn-typescript`: `npm install acorn@^8.9.0` or `yarn add acorn@^8.9.0`.
Pass `{ dts: true }` to the plugin function: `acorn.Parser.extend(tsPlugin({ dts: true }))`.Add `{ locations: true }` to the options object passed to the `parse` method.Ensure `acorn@^8.9.0` is installed: `npm install acorn@^8.9.0`.
Verify that `acorn.Parser.extend(tsPlugin())` is correctly applied to your parser instance and that `ecmaVersion: 'latest'` and `sourceType: 'module'` are set if applicable.
Use a default import: `import tsPlugin from 'acorn-typescript'`.