Convert Figma logo to code with AI

dsherret logots-morph

TypeScript Compiler API wrapper for static analysis and programmatic code changes.

4,869
194
4,869
270

Top Related Projects

100,112

TypeScript is a superset of JavaScript that compiles to clean JavaScript output.

Converts JavaScript to TypeScript and TypeScript to better TypeScript. 🧫

Over TypeScript tool to use custom transformers in the tsconfig.json

12,836

TypeScript execution and REPL for node.js

Quick Overview

ts-morph is a powerful TypeScript compiler API wrapper that simplifies working with TypeScript ASTs (Abstract Syntax Trees). It provides an intuitive, object-oriented API for analyzing, manipulating, and generating TypeScript code programmatically.

Pros

  • Easy-to-use API for working with TypeScript ASTs
  • Extensive documentation and examples
  • Supports both reading and writing TypeScript files
  • Actively maintained and regularly updated

Cons

  • Learning curve for users unfamiliar with ASTs
  • Can be memory-intensive for large projects
  • Limited support for certain advanced TypeScript features
  • Slower performance compared to direct use of the TypeScript compiler API

Code Examples

  1. Creating a new TypeScript file:
import { Project } from "ts-morph";

const project = new Project();
const sourceFile = project.createSourceFile("example.ts", "const x: number = 5;");
console.log(sourceFile.getFullText());
  1. Adding a method to an existing class:
import { Project } from "ts-morph";

const project = new Project();
const sourceFile = project.addSourceFileAtPath("MyClass.ts");
const myClass = sourceFile.getClassOrThrow("MyClass");

myClass.addMethod({
  name: "newMethod",
  parameters: [{ name: "param", type: "string" }],
  returnType: "void",
  statements: "console.log(param);"
});

sourceFile.save();
  1. Renaming a variable:
import { Project } from "ts-morph";

const project = new Project();
const sourceFile = project.addSourceFileAtPath("example.ts");
const variable = sourceFile.getVariableDeclarationOrThrow("oldName");

variable.rename("newName");
sourceFile.save();

Getting Started

To start using ts-morph, follow these steps:

  1. Install ts-morph in your project:

    npm install ts-morph
    
  2. Import and use ts-morph in your TypeScript file:

    import { Project } from "ts-morph";
    
    const project = new Project();
    const sourceFile = project.addSourceFileAtPath("path/to/your/file.ts");
    
    // Start working with the AST
    const classes = sourceFile.getClasses();
    console.log(`Number of classes: ${classes.length}`);
    
  3. Refer to the official documentation for more detailed usage instructions and API reference.

Competitor Comparisons

100,112

TypeScript is a superset of JavaScript that compiles to clean JavaScript output.

Pros of TypeScript

  • Official Microsoft project with extensive resources and community support
  • Comprehensive language features and continuous improvements
  • Seamless integration with Visual Studio and other Microsoft tools

Cons of TypeScript

  • Large codebase can be overwhelming for contributors
  • Slower release cycle due to rigorous testing and compatibility checks
  • Limited focus on programmatic manipulation of TypeScript code

Code Comparison

TypeScript (AST manipulation):

const sourceFile = ts.createSourceFile("example.ts", sourceCode, ts.ScriptTarget.Latest);
const printer = ts.createPrinter();
const result = printer.printNode(ts.EmitHint.Unspecified, sourceFile, sourceFile);

ts-morph (AST manipulation):

const project = new Project();
const sourceFile = project.createSourceFile("example.ts", sourceCode);
const result = sourceFile.getFullText();

Key Differences

  • TypeScript focuses on language development and compiler implementation
  • ts-morph provides a higher-level API for TypeScript AST manipulation
  • ts-morph offers easier programmatic code generation and refactoring
  • TypeScript has broader scope, while ts-morph specializes in code transformation

Use Cases

  • Choose TypeScript for general TypeScript development and compiler usage
  • Opt for ts-morph when working on code analysis, generation, or refactoring tools

Converts JavaScript to TypeScript and TypeScript to better TypeScript. 🧫

Pros of TypeStat

  • Focused on automated type improvements and migrations
  • Provides specific fixes for common TypeScript issues
  • Offers a CLI tool for easy integration into workflows

Cons of TypeStat

  • More limited scope compared to ts-morph's broader capabilities
  • Less flexibility for custom transformations
  • Smaller community and fewer updates

Code Comparison

TypeStat example:

typestat --fixNoImplicitAny --fixNoImplicitThis

ts-morph example:

import { Project } from "ts-morph";

const project = new Project();
const sourceFile = project.addSourceFileAtPath("path/to/file.ts");
sourceFile.getClasses().forEach(cls => {
    // Custom transformation logic
});

TypeStat is designed for specific type-related improvements, while ts-morph provides a more comprehensive API for TypeScript AST manipulation. TypeStat offers a simpler CLI interface for quick fixes, whereas ts-morph allows for more complex and customized transformations through its programmatic API. ts-morph has a larger community and more frequent updates, making it more suitable for diverse TypeScript-related tasks beyond just type improvements.

Over TypeScript tool to use custom transformers in the tsconfig.json

Pros of ttypescript

  • Focuses specifically on TypeScript transformers, allowing for more targeted modifications
  • Simpler setup and usage for TypeScript-specific transformations
  • Integrates seamlessly with existing TypeScript tooling

Cons of ttypescript

  • Limited scope compared to ts-morph's broader TypeScript manipulation capabilities
  • Less active development and community support
  • Fewer features for complex AST manipulations and code generation

Code Comparison

ts-morph:

import { Project } from "ts-morph";

const project = new Project();
const sourceFile = project.createSourceFile("file.ts", "const x = 5;");
sourceFile.getVariableDeclaration("x").setInitializer("10");

ttypescript:

import * as ts from "typescript";

function transformer(program: ts.Program): ts.TransformerFactory<ts.SourceFile> {
  return (context) => (file) => ts.visitNode(file, visit);
}

Both libraries provide ways to manipulate TypeScript code, but ts-morph offers a higher-level API for general-purpose TypeScript manipulation, while ttypescript focuses specifically on creating custom TypeScript transformers. ts-morph is more versatile and feature-rich, while ttypescript is more specialized for transformer-based modifications.

12,836

TypeScript execution and REPL for node.js

Pros of ts-node

  • Allows direct execution of TypeScript files without separate compilation step
  • Integrates seamlessly with Node.js ecosystem and tools
  • Supports REPL for interactive TypeScript development

Cons of ts-node

  • Limited to runtime execution and doesn't provide advanced AST manipulation
  • May have performance overhead for large projects due to on-the-fly compilation
  • Doesn't offer extensive refactoring or code analysis capabilities

Code Comparison

ts-node usage:

// Execute TypeScript file directly
ts-node script.ts

// Use in Node.js require
require('ts-node').register();
require('./script.ts');

ts-morph usage:

import { Project } from "ts-morph";

const project = new Project();
const sourceFile = project.createSourceFile("file.ts", "let x: string = 'Hello';");
const variable = sourceFile.getVariableDeclarationOrThrow("x");
console.log(variable.getType().getText()); // outputs: string

Key Differences

ts-node focuses on runtime execution of TypeScript in Node.js environments, while ts-morph is a powerful library for programmatically manipulating TypeScript code, offering advanced AST operations, refactoring, and code analysis capabilities. ts-node is ideal for running TypeScript applications directly, whereas ts-morph is better suited for building development tools, linters, or performing complex code transformations.

Convert Figma logo designs to code with AI

Visual Copilot

Introducing Visual Copilot: A new AI model to turn Figma designs to high quality code using your components.

Try Visual Copilot

README

ts-morph

CI

Monorepo for ts-morph and related projects.

Packages

  • ts-morph - TypeScript Compiler API wrapper. Provides an easier way to programmatically navigate and manipulate TypeScript and JavaScript code.
  • @ts-morph/bootstrap - Separate library for getting quickly setup with the Compiler API.

Resources

NPM DownloadsLast 30 Days