ts-morph
Navigate and manipulate TypeScript code with a friendlier compiler API wrapper
Repository Health
Technical Analysis
ts-morph wraps the raw TypeScript Compiler API in an object-oriented, discoverable layer, exposing a Project object that loads source files and lets you navigate, query, and programmatically modify TypeScript/JavaScript ASTs without wrestling with the compiler’s low-level node factories directly. Common tasks — adding an import, renaming a symbol project-wide, extracting an interface, or rewriting call expressions — become a handful of chainable method calls instead of manual AST construction and printer plumbing.
Because it sits directly on top of the real TypeScript compiler, ts-morph gets full type-checking and language-service accuracy for free, which is why it’s a common backbone for codemods, custom linters, doc generators, and code-generation tooling in TypeScript projects.
What You Get
- A
ProjectAPI for loading TypeScript/JavaScript files from disk,tsconfig.json, or an in-memory virtual file system - Wrapped, chainable AST node classes (
SourceFile,ClassDeclaration,InterfaceDeclaration, etc.) with discoverable getters and mutators - Project-wide symbol renaming and reference finding backed by the real TypeScript language service
- Code generation helpers for adding/removing imports, classes, methods, and properties without manual AST/printer code
- A
ts-morph.d.ts-backed structure API for describing code declaratively via plain objects instead of imperative builder calls
Common Use Cases
- Writing codemods that rewrite deprecated API usage across a large TypeScript codebase
- Generating TypeScript declaration files, client SDKs, or boilerplate from schemas or other source-of-truth definitions
- Building custom static-analysis tools or linters that need type-checked AST traversal
- Automating large-scale refactors (renaming exports, extracting interfaces, restructuring modules) safely across many files
Under The Hood
Architecture - The packages/ts-morph package’s src/compiler directory wraps every relevant TypeScript compiler AST node in a corresponding ts-morph class that holds a reference to the underlying ts.Node and the owning Project’s ts.Program/language service, so navigation methods (getParent(), getChildren()) and mutation methods (.rename(), .remove()) can delegate to either the raw AST or the language service as needed; src/codeBlockWriter provides an indentation-aware string builder used internally (and exposed) for generating source text.
Tech Stack - Pure TypeScript, organized as a monorepo (packages/ts-morph, packages/bootstrap, packages/common, packages/scripts) built with Deno tooling (deno.json/deno.lock) alongside npm packaging, bundled via Rollup (rollup.config.mjs), with a code-generation step (scripts/generation) that produces much of the wrapper-class boilerplate directly from the TypeScript compiler’s own type definitions to stay in sync across TS versions.
Code Quality - Tests run under Mocha (.mocharc.yml) with dedicated typeCheckDocumentation.ts and typeCheckLibrary.ts scripts that verify both the README’s code samples and the library’s own types actually compile, catching documentation drift that plain unit tests would miss; the breaking-changes.md file tracks API changes explicitly release over release, a sign of deliberate API stewardship for a library with a large public surface.
API Design - The chainable, getter-heavy API (sourceFile.getClasses()[0].getMethods()) mirrors how developers already think about ASTs, and the additional “structure” API (describing nodes as plain object literals) offers a declarative alternative to imperative builder calls for generation-heavy use cases, trading some API surface size for genuinely improved discoverability over the raw compiler API.
Used by 28 apps in this directory
Novu
Developer Tools
Open-source communication infrastructure that connects your products and AI agents to every channel your users live on — Inbox, Email, SMS, Push, Chat, and more.
OnetimeSecret
Security
Generate self-destructing single-use links to share passwords and sensitive data without leaving traces in inboxes or chat logs.
OpenKnowledge
Code Editors · Knowledge Management · Note Taking
A beautiful, local-first markdown IDE that turns any git repo into a live collaborative workspace for humans and AI coding agents like Claude, Codex, and OpenCode.
Payload CMS
Blogging · CMS · Developer Tools
The open-source, Next.js-native headless CMS that lives inside your /app folder and gives you a full TypeScript backend instantly.
Polar
Developer Tools · Ecommerce · Invoicing Finance
Open source payments infrastructure that turns software into a business — subscriptions, usage-based billing, digital products, and merchant-of-record compliance in one platform.
Sanity
CMS
Open-source headless CMS with a fully customizable React Studio, real-time collaborative editing, structured content modeling, and GROQ query language
Supabase
Authentication · Databases · Developer Tools
The open-source Postgres development platform that replaces Firebase with authentication, real-time APIs, edge functions, storage, and vector embeddings — all built on PostgreSQL.
twenty
CRM
The open-source CRM you build, ship, and version like the rest of your stack — with customizable objects, AI agents, and a TypeScript SDK.
Umami
Analytics
Privacy-first web analytics that respects your users — self-hosted, cookieless, and GDPR compliant out of the box.