Small utility to fix common js->ts issues in order to assist in migrating a codebase
Small utility that I wrote to script converting a JS codebase to TypeScript, while trying to solve some of the common TypeScript errors that will be received upon such a conversion.
The utility performs the following transformations:
.jsfiles to
.ts
Note: because this utility utilizes the TypeScript Language Service to perform the look-ups for #3, it may take a long time to run. For a small project, expect a few minutes. For a larger project, it could take tens of minutes. Still much better than the days/weeks it could take to fix an entire codebase by hand :)
For #2 above, the utility basically looks at any
thisproperty accessed by a JS class, and fills in the appropriate TypeScript property declarations. Take this
.jsinput source file as an example:
class Super { someMethod() { this.superProp = 1; } }class Sub extends Super { someMethod() { this.superProp = 2; this.subProp = 2; } }
The above JS classes are replaced with the following TS classes:
class Super { public superProp: any; //Note: properties used when
thisis assigned to another variable are also found for purposes of creating property declarations. Example:class MyClass { myMethod() { var that = this;that.something; //
For #3 above, parameters are marked as 'optional' when there are callers that don't provide all of them. For example, the following JavaScript:
function myFunction( arg1, arg2 ) { // ... }myFunction( 1 ); // only provide arg1
Will be transformed to the following TypeScript:
function myFunction( arg1, arg2? ) { //Goal
The goal of this utility is to simply make the
.jscode compilable under the TypeScript compiler, so simply adding the property declarations typed asanywas the quickest option there. The utility may look at property initializers in the future to determine a better type.If you have other types of compiler errors that you think might be able to be transformed by this utility, please feel free to raise an issue (or pull request!)
Hopefully you only need to use this utility once, but if it saved you time, please star it so that I know it helped you out :)
Fair Warning
This utility makes modifications to the directory that you pass it. Make sure you are in a clean git (or other VCS) state before running it in case you need to revert!
Running the Utility from the CLI
npx js-to-ts-converter ./path/to/js/filesIf you would prefer to install the CLI globally, do this:
npm install --global js-to-ts-converterjs-to-ts-converter ./path/to/js/files
Running the Utility from Node
TypeScript:
import { convertJsToTs, convertJsToTsSync } from 'js-to-ts-converter';// Async convertJsToTs( 'path/to/js/files' ).then( () => console.log( 'Done!' ), ( err ) => console.log( 'Error: ', err ); );
// Sync convertJsToTsSync( 'path/to/js/files' ); console.log( 'Done!' );
JavaScript:
const { convertJsToTs, convertJsToTsSync } = require( 'js-to-ts-converter' );// Async convertJsToTs( 'path/to/js/files' ).then( () => console.log( 'Done!' ), ( err ) => console.log( 'Error: ', err ); );
// Sync convertJsToTsSync( 'path/to/js/files' ); console.log( 'Done!' );
Developing
Make sure you have Node.js installed.
Clone the git repo:
git clone https://github.com/gregjacobs/js-to-ts-converter.gitcd js-to-ts-converter
Install dependencies:
npm installRun Tests:
npm test