ts-morph (formerly ts-simple-ast) is a robust library that wraps the TypeScript Compiler API, offering a more user-friendly and programmatic way to navigate, analyze, and manipulate TypeScript and JavaScript code. It provides an in-memory file system where all changes are tracked until explicitly saved, allowing for complex refactoring and code generation tasks. The library maintains strong compatibility with recent TypeScript versions, often releasing new major versions shortly after a new TypeScript compiler release. The current stable version is 28.0.0, which supports TypeScript 6.0. Key differentiators include its extensive wrapper API, enabling easy traversal and modification of AST nodes, and its ability to fall back to the raw `compilerNode` when advanced compiler API access is needed, providing full flexibility for complex scenarios.
npm install ts-morphVerified import paths — ran on the pinned version, not inferred.
Demonstrates initializing a project, adding and creating source files, navigating the AST, manipulating a class, and saving changes.
Always check ts-morph's release notes (e.g., CHANGELOG.md) for its supported TypeScript version before upgrading. Ensure your `typescript` dependency in `package.json` aligns with ts-morph's requirements.
Review the TypeScript 6.0 release notes (e.g., 'https://devblogs.microsoft.com/typescript/announcing-typescript-6-0/') and ts-morph's breaking changes documentation for specific adjustments needed in your codebase.
If you chained calls after `forgetDescendants()`, you will need to re-structure your code to explicitly get the node before making subsequent calls, as it no longer returns `this`.
Upgrade to ts-morph `27.0.2` or higher to resolve browser compatibility issues.
Ensure `ts-morph` is installed (`npm install ts-morph`). If using CommonJS, use `const { Project } = require('ts-morph');`. If using ES modules (recommended), ensure `"type": "module"` is set in `package.json` or use `.mjs` files.Adjust your project's `typescript` dependency in `package.json` to match the version required by `ts-morph` (check ts-morph's `package.json` or documentation). Then run `npm install`.
After significant AST manipulations (e.g., deleting a node, moving files), re-obtain the relevant nodes from the project or their parents. If a node is no longer needed, ensure you don't try to access its properties or methods.
No dependency data recorded yet.