Skip to content
Refactor TypScript code programmatically using codemods
Branch: master
Clone or download
Latest commit 2734704 Oct 1, 2019
Type Name Latest commit message Commit time
Failed to load latest commit information.
examples Initial commit Sep 30, 2019
src WIP Sep 30, 2019
.gitignore WIP Sep 30, 2019
LICENSE Initial commit Sep 30, 2019 Update Oct 1, 2019
_config.yml Set theme jekyll-theme-minimal Oct 1, 2019
ast-viewer.jpg Add files via upload Oct 1, 2019
package.json WIP Sep 30, 2019
tsconfig.json WIP Sep 30, 2019


Inspired by the talk from Cristina Bernardis about jscodeshift at JSDayIE I have released tsmod. A library that allows us to write automated refactoring code modifications powered by David Sherret's ts-morph.

What is this about?

If you have a very large codebase and you want to change something across many files this tool will allow you to write a script that will do the work for you. This is a good idea because it can save you time but also because it can be used by other members of the team as a valuable source of information. The transform scripts can also be shared online as open source. A common example is a migration script for a breaking change in the API of a framework. You can release the new version of the framework together with the transform scripts to help the users of the framework to upgrade their version with ease.


You can install this module as a global dependency using npm:

npm install -g tsmod

Please note that if you have never used TypeScript or ts-node you will also need them:

npm install -g typescript ts-node

The typescript module is the TypeScript compiler and the ts-node module is a version of Node.js that can work directly with TypeScript files (.ts) instead of using JavaScript files (.js).


The following command applies the transform var_to_const_transform.ts to the files fileA.ts and fileB.ts:

tsmod -t var_to_const_transform.ts fileA.ts fileB.ts

Please Note: A TypeScript compiler configuration file (tsconfig.json) file is expected in the current directory when you run the previous command.

Transform example

The transforms are powered by the ts-morph API. You can learn more about the API at

The following example changes all var variable declarations into const variable declarations:

import { SourceFile, SyntaxKind, VariableDeclarationKind } from "ts-morph";

export const varToConstTransform = (file: SourceFile, transformArgs: {}) => {
  // Find all variable declarations in source file
  const variableStatements = file.getDescendantsOfKind(
  // Change var for const for each statement
  variableStatements.forEach(variableStatement => {
    const declarationKind = variableStatement.getDeclarationKind();
    if (declarationKind === VariableDeclarationKind.Var) {
  // Return source code
  const updatedSourceCode = file.getText();
  return updatedSourceCode;

The code is represented using a data structure known as Abstract Syntax Tree (AST). You can navigate and modify the AST to generate updated code. You can visit to visualize the AST if you need help navigating it.


For additional help use the following:

tsmod -h
You can’t perform that action at this time.