Acorn TypeScript Parser Plugin
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.
Common errors
-
Error: The 'locations' option must be enabled for acorn-typescript.
cause `locations: true` was not included in the parser options.fixAdd `{ locations: true }` to the options object passed to the `parse` method. -
TypeError: Cannot read properties of undefined (reading 'extend')
cause The `acorn` package might not be installed, or an incompatible version is present, preventing the `Parser.extend` method from being accessed correctly.fixEnsure `acorn@^8.9.0` is installed: `npm install acorn@^8.9.0`. -
SyntaxError: Unexpected token (some_typescript_keyword)
cause Attempting to parse TypeScript specific syntax (e.g., `type`, `interface`, decorators) without correctly extending Acorn with `acorn-typescript` or with incorrect parser options.fixVerify that `acorn.Parser.extend(tsPlugin())` is correctly applied to your parser instance and that `ecmaVersion: 'latest'` and `sourceType: 'module'` are set if applicable. -
TypeError: acorn_typescript__WEBPACK_IMPORTED_MODULE_1___default is not a function
cause Attempted to import the `acorn-typescript` plugin incorrectly, typically using `import { tsPlugin } from 'acorn-typescript'` instead of a default import.fixUse a default import: `import tsPlugin from 'acorn-typescript'`.
Warnings
- gotcha The `locations: true` option is mandatory when using `acorn-typescript`. Failing to enable it will result in parsing errors or unexpected AST structures.
- breaking Starting from `v1.3.7`, `acorn-typescript` added explicit validation for `locations` when using `static parse` or `static parseExpression`. While the requirement existed before, this version likely makes the omission an explicit error or warning during parsing.
- gotcha This package is a plugin for Acorn. It requires `acorn` as a peer dependency, specifically `acorn@>=8.9.0`. Ensure a compatible version of `acorn` is installed in your project to avoid runtime errors.
- gotcha When parsing TypeScript declaration files (`.d.ts`) or code within `declare module` blocks, you must enable the `dts: true` option in the plugin configuration to handle ambient context-specific syntax correctly.
Install
-
npm install acorn-typescript -
yarn add acorn-typescript -
pnpm add acorn-typescript
Imports
- acorn
const acorn = require('acorn')import * as acorn from 'acorn'
- tsPlugin
import { tsPlugin } from 'acorn-typescript'import tsPlugin from 'acorn-typescript'
- Parser.extend
acorn.extend(tsPlugin())
acorn.Parser.extend(tsPlugin())
Quickstart
import * as acorn from 'acorn';
import tsPlugin from 'acorn-typescript';
// Example 1: Basic TypeScript parsing
const code1 = `
const a: number = 1;
type MyType = string;
export {
a,
type MyType as RenamedType
};
class MyClass { accessor b = 'hello'; }
`;
const ast1 = acorn.Parser.extend(tsPlugin()).parse(code1, {
sourceType: 'module',
ecmaVersion: 'latest',
locations: true // Required for acorn-typescript
});
console.log('Parsed AST 1 (basic):', JSON.stringify(ast1.body[0], null, 2));
// Example 2: Parsing within a TypeScript ambient context (.d.ts files)
const code2 = `
declare module 'my-module' {
interface MyInterface {
name: string;
id: number;
}
}
`;
const ast2 = acorn.Parser.extend(tsPlugin({ dts: true })).parse(code2, {
sourceType: 'module',
ecmaVersion: 'latest',
locations: true // Still required
});
console.log('Parsed AST 2 (dts context):', JSON.stringify(ast2.body[0], null, 2));