ts-morph
TypeScript compiler API wrapper that makes programmatic TypeScript code analysis and manipulation accessible. ts-morph wraps TypeScript's complex compiler API in a simpler, navigable object model — read source files, traverse AST nodes, modify code, add/remove declarations, and generate TypeScript programmatically. Used for codemods, code generators, documentation extractors, and static analysis tools.
Score Breakdown
⚙ Agent Friendliness
🔒 Security
Local code analysis — no security concerns. Code modification tools can introduce vulnerabilities if used carelessly; review generated/modified code.
⚡ Reliability
Best When
You need to programmatically analyze or modify TypeScript code at the AST level — codemods, code generators, or documentation extractors.
Avoid When
You need simple text transformations or runtime type checking — ts-morph's AST approach is overkill for basic string replacement.
Use Cases
- • Build TypeScript codemods that programmatically refactor code across large codebases — rename types, migrate APIs, update patterns
- • Generate TypeScript code programmatically from schemas, databases, or configuration in agent scaffolding tools
- • Extract TypeScript documentation and type information for API documentation generators
- • Build static analysis tools that traverse TypeScript ASTs to find patterns, anti-patterns, or metrics
- • Write type-safe code transformation pipelines in agent systems that modify TypeScript source files
Not For
- • JavaScript-only projects — ts-morph requires TypeScript
- • Simple text/regex-based code changes — ts-morph is powerful but heavyweight for simple string replacements
- • Runtime type checking — ts-morph is a static analysis tool; use Zod or TypeBox for runtime validation
Interface
Authentication
No authentication — local code analysis library.
Pricing
MIT-licensed open source library.
Agent Metadata
Known Gotchas
- ⚠ ts-morph creates an in-memory TypeScript project — loading large codebases consumes significant memory and time for initial type checking; disable type checking if not needed with useInMemoryFileSystem or skipLibCheck
- ⚠ Node manipulations are order-sensitive — removing a node while iterating its parent's children causes index shift issues; collect nodes first, then modify
- ⚠ ts-morph version must be compatible with the TypeScript version — check peerDependencies; mismatches cause compiler API incompatibilities
- ⚠ Save order matters when writing generated files — ts-morph's in-memory model must be explicitly saved to disk via project.save() or sourceFile.save()
- ⚠ Type resolution requires the full project context — analyzing a single file without the rest of the project may produce incorrect or missing type information
- ⚠ Formatted output requires explicit formatting — ts-morph generates syntactically correct but unformatted code; run Prettier/Biome after generation
Alternatives
Full Evaluation Report
Comprehensive deep-dive: security analysis, reliability audit, agent experience review, cost modeling, competitive positioning, and improvement roadmap for ts-morph.
AI-powered analysis · PDF + markdown · Delivered within 30 minutes
Package Brief
Quick verdict, integration guide, cost projections, gotchas with workarounds, and alternatives comparison.
Delivered within 10 minutes
Score Monitoring
Get alerted when this package's AF, security, or reliability scores change significantly. Stay ahead of regressions.
Continuous monitoring
Scores are editorial opinions as of 2026-03-07.