DEV.co
AI Coding Agents · dsherret

ts-morph

ts-morph is a TypeScript wrapper around the TypeScript Compiler API that simplifies programmatic code navigation and transformation. It lets developers read, analyze, and modify TypeScript and JavaScript code at scale without dealing with low-level compiler internals.

Source: GitHub — github.com/dsherret/ts-morph
6.1k
GitHub stars
234
Forks
TypeScript
Primary language
MIT
License (OSI-approved)

Key facts

Objective fields from the source. Values we can't verify are shown as “Unknown” rather than guessed.

FieldValue
Repositorydsherret/ts-morph
Ownerdsherret
Primary languageTypeScript
LicenseMIT — OSI-approved
Stars6.1k
Forks234
Open issues289
Latest release28.0.0 (2026-04-12)
Last updated2026-04-12
Sourcehttps://github.com/dsherret/ts-morph

What ts-morph is

ts-morph abstracts the TypeScript Compiler API into a higher-level object model for AST traversal, querying, and mutation. It supports static analysis workflows, code generation, and refactoring tasks with a fluent API built on top of the official compiler.

Quickstart

Get the ts-morph source

Clone the repository and explore it locally.

terminalbash
git clone https://github.com/dsherret/ts-morph.gitcd ts-morph# follow the project's README for install & configuration

Need it deployed, integrated, or customized instead? DEV.co ships production installs.

Best use cases

Automated Code Refactoring & Codemod Tooling

Build internal tools to rename symbols, restructure imports, apply linting fixes, or perform large-scale code migrations across TypeScript/JavaScript projects programmatically.

Static Analysis & AST-Based Linting

Create custom linters, pattern detectors, or compliance checkers that traverse and analyze code structure without relying on regex or brittle text parsing.

Code Generation & Template Expansion

Programmatically generate boilerplate code, API client SDKs, or scaffolds from templates, with full type-safety and AST-level manipulation rather than string concatenation.

Implementation considerations

  • ts-morph requires installing the TypeScript Compiler as a peer dependency; ensure version compatibility with your project's TypeScript version.
  • AST manipulation requires careful understanding of the TypeScript type hierarchy; refer to official TypeScript Compiler API docs alongside ts-morph's API.
  • Memory usage can be significant when loading large projects into memory; consider batching or streaming approaches for very large codebases.
  • Roundtrip fidelity: verify that modified code preserves formatting, comments, and whitespace as expected for your use case.
  • Test coverage of your codemod/refactoring logic is critical; AST changes can silently introduce type errors or logic bugs.

When to avoid it — and what to weigh

  • Simple String Find-and-Replace Suffices — If you only need basic text substitution or regex matching, ts-morph is over-engineered. Standard text tools are faster and simpler.
  • Runtime Code Evaluation Required — ts-morph is a static analysis library; it does not execute code or provide runtime introspection. For dynamic analysis, use Node.js vm or similar.
  • Non-TypeScript/JavaScript Codebases — ts-morph is TypeScript Compiler API–specific. For Python, Go, Java, or other languages, use language-specific AST libraries (e.g., ast, tree-sitter).
  • Extreme Performance at Scale (100k+ Files) — Unknown whether ts-morph scales efficiently to very large projects. Compiler API performance characteristics and memory usage at scale require testing.

License & commercial use

ts-morph is licensed under the MIT License, a permissive OSI-approved license that permits commercial use, modification, and distribution with minimal restrictions.

MIT License explicitly permits commercial use. You may use ts-morph in proprietary products and internal tools without license restrictions. Include a copy of the MIT license and original attribution in distributions. No warranty or liability indemnity is provided by the licensor.

DEV.co evaluation signals

Editorial assessment — not user reviews. Directional, with an explicit confidence level.

SignalAssessment
MaintenanceActive
DocumentationAdequate
License clarityClear
Deployment complexityLow
DEV.co fitGood
Assessment confidenceHigh
Security considerations

ts-morph parses and manipulates code; no built-in input validation. Ensure source code is from trusted origins if processing untrusted inputs. No obvious injection vectors in the library itself, but arbitrary code generation or eval workflows introduce risk. Regularly update TypeScript dependency for security patches.

Alternatives to consider

babel

Industry standard for JavaScript/TypeScript AST transformation; larger ecosystem and more mature for code generation, but steeper learning curve and less TypeScript-native.

recast

Focuses on roundtrip code preservation and pretty-printing; lighter-weight but fewer high-level abstractions and less TypeScript support than ts-morph.

tree-sitter

Language-agnostic parser; faster incremental parsing and multi-language support, but requires learning a different API and lacks TypeScript Compiler semantic understanding.

Software development agency

Build on ts-morph with DEV.co software developers

ts-morph simplifies AST-based code transformation. Assess your project's TypeScript version, peer dependencies, and scalability needs. Consider our custom development services if you need embedded tooling or large-scale codemod infrastructure.

Talk to DEV.co

Related open-source tools

Surfaced by semantic similarity across the DEV.co open-source index.

Related on DEV.co

Explore the category and the services that help you build with it.

ts-morph FAQ

Does ts-morph work with JavaScript files?
Yes. ts-morph wraps the TypeScript Compiler API, which handles both .ts and .js files. Type information is inferred or ignored as needed.
Can I use ts-morph to format code?
ts-morph provides AST manipulation but does not handle pretty-printing or code formatting. For formatting, use Prettier or ts-morph in combination with a formatter.
What is the performance for large codebases?
Unknown. Loading 100k+ files into memory and traversing the full AST may be slow or memory-intensive; requires benchmarking for your specific use case.
Is ts-morph suitable for IDE/language server plugins?
Potentially, if your plugin runs on Node.js. ts-morph relies on the TypeScript Compiler API and is not optimized for real-time or incremental analysis; investigate latency and memory overhead.

Work with a software development agency

From first prototype to production, DEV.co delivers software development services around tools like ts-morph. Our software development agency staffs experienced software developers and web developers for custom software development, web development, integrations, and ongoing support across ai coding agents and beyond.

Ready to Build TypeScript Codemods & Refactoring Tools?

ts-morph simplifies AST-based code transformation. Assess your project's TypeScript version, peer dependencies, and scalability needs. Consider our custom development services if you need embedded tooling or large-scale codemod infrastructure.