Skip to main content

typescript()

Parser
required
oxcParser for Node and Bun, or the parser that await createWasmParser() resolves to for browsers, workers and edge runtimes. typescript() throws a readable error if you pass something else, such as the Promise itself. See Embedding.
(TsPlugin | TsPlugin[])[]
Library and codebase plugins, applied after the built-in std plugin. Nested arrays are flattened. Later plugins win.
({ engine, path }) => void
Runs before each file is narrated. Use it to install cross-file hooks, such as engine.typeOfCall, which lets the engine look up the declared return type of a function defined in another file.
(path) => Record<string, string> | undefined
A fallback source of package dependencies per file. Normally you pass a dependency index to createNarrator instead, which takes precedence over this option. See Dependency detection.
The language handles .ts, .tsx, .js, .jsx, .mjs and .cjs, but not .d.ts. Its ecosystems are ["npm"]. Its extra engine(locale) method creates a bare engine, which is handy in tools and tests.

createWasmParser()

Loads oxc’s WASM build, which ships inside @usenarrator/lang-ts, and resolves to a synchronous Parser. Create it once and reuse it: every call starts a new WASM instance.
URL | string | Response | bytes | WebAssembly.Module
Where the .wasm file comes from. Leave it out in Node, Bun and bundlers that turn new URL(…, import.meta.url) into an asset, such as Vite. Pass a compiled module on Cloudflare Workers, imported from @usenarrator/lang-ts/wasm.wasm, and a URL inside a browser extension. If the value is not a WebAssembly file, for example an HTML page served instead of the .wasm, the error says so.

Syntax errors and failing rules

A file with a syntax error is still narrated. oxc recovers from some errors on its own, such as a return outside a function, and the file reads as usual. When an error stops the parser, the engine blanks out the fewest whole lines that let the rest parse (falling back to whole top-level statements), narrates everything else, and puts a note where the gap is, such as “Line 12 has a syntax error, so it’s left out.” The narration’s errors still counts every error the parser reported, each one is listed in problems with source: "parser", and the note’s text comes from the locale’s statements.unreadable phrase. A plugin rule that throws or returns something unusable is skipped as if it had returned null. Each one is listed in problems with source: "plugin", severity: "warning", the plugin name and the rule, and counted in stats.ruleErrors. See When a rule fails.

The Engine inside rules

Every rule receives the engine as e. These are the methods plugins in the repository use, roughly from most to least common.

Rendering expressions

The current file

These describe the file being narrated. They are set before any rule runs.

Names

Arguments, objects and functions

Emitting lines (statement rules)

State

The Engine is a large class and most of its members are public. The methods above are the ones plugins rely on today. Others are internal to the walker and may change without notice.