• Stars
    star
    232
  • Rank 172,847 (Top 4 %)
  • Language
    TypeScript
  • License
    MIT License
  • Created over 6 years ago
  • Updated about 1 year ago

Reviews

There are no reviews yet. Be the first to send feedback to the community and the maintainers!

Repository Details

Small utility to fix common js->ts issues in order to assist in migrating a codebase

js-to-ts-converter

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:

  1. Renames .js files to .ts
  2. Adds property declarations to ES6 classes so that they are compilable by the TypeScript compiler (see below).
  3. Any function calls that provide fewer arguments than the declared parameters in the function will cause the remaining parameters to be marked as optional for that function. This solves TS errors like "Expected 3 arguments, but got 2"

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 this property accessed by a JS class, and fills in the appropriate TypeScript property declarations. Take this .js input 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;   // <-- added

    someMethod() {
        this.superProp = 1;
    }
}


class Sub extends Super {
    public subProp: any;    // <-- added

    someMethod() {
        this.superProp = 2;
        this.subProp = 2;
    }
}

Note: properties used when this is assigned to another variable are also found for purposes of creating property declarations. Example:

class MyClass {
    myMethod() {
        var that = this;
        
        that.something;  // <-- 'something' parsed as a class property
    }
}

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? ) {  // <-- arg2 marked as optional
	// ...
}

myFunction( 1 );

Goal

The goal of this utility is to simply make the .js code compilable under the TypeScript compiler, so simply adding the property declarations typed as any was 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/files

If you would prefer to install the CLI globally, do this:

npm install --global js-to-ts-converter

js-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.git

cd js-to-ts-converter

Install dependencies:

npm install

Run Tests:

npm test

More Repositories

1

Autolinker.js

Utility to Automatically Link URLs, Email Addresses, Phone Numbers, Twitter handles, and Hashtags in a given block of text/HTML
TypeScript
1,469
star
2

Class.js

Simple and powerful utility to allow for the easy use of OOP in JS which includes inheritance, mixins, abstract classes, inherited statics, and more.
JavaScript
22
star
3

jquery-fillmore

Fillmore is a jQuery plugin that allows you to add a dynamically-resized background image to any page, or any given element. The image will stretch to fit the page / element, and will automatically resize as the window size changes.
JavaScript
14
star
4

jQuery-GUI

JavaScript
5
star
5

Kevlar.js

JavaScript
5
star
6

Angular-Architecture

Advanced Architecture and High-Level Guides for Large-Scale AngularJS App Development
CSS
3
star
7

WebAppBuilder

A JavaScript and CSS build tool, which can combine (concatenate) and minify a large number of JavaScript and CSS files, in dependency order.
Java
3
star
8

angular-aot-fixer

Fixes common issues in an Angular codebase that prevent AOT compilation
TypeScript
2
star
9

jquery-windowresizeprotector

Protects your code from accidentally unbinding *all* window resize events by accident (from say, passing undefined as the handler), thus affecting much more code than intended.
JavaScript
2
star
10

closure-compiler-requirejs-POC

Proof of concept for getting the Google Closure Compiler to work with RequireJS modules
JavaScript
1
star
11

rules-nodejs-supports-workers-issue

Reproduction for ts_project with supports_workers not failing the build when there are TypeScript errors
Starlark
1
star
12

rush-test

Testing Rush vs. Nx
TypeScript
1
star
13

node-exec-cwd

Repository for reporting a possible bug in Node.js
JavaScript
1
star
14

gregjacobs.github.io

Repository for my personal website.
HTML
1
star
15

equations

Small test program for resolving and expanding dependent variables in a set of equations.
JavaScript
1
star
16

ExttestToJasmineConverter

A project to convert my old Ext.Test/YUI unit tests into Jasmine unit tests.
JavaScript
1
star
17

MultiScrollTest

Testing out multiple outer/inner (parallax) scrolling ideas.
JavaScript
1
star
18

carousel-poc

JavaScript
1
star
19

angular-promise-cache-2

JavaScript
1
star
20

bazel-test

Testing Bazel with MS Defender performance issue
TypeScript
1
star
21

Observable.js

JavaScript
1
star
22

nx-test

Test repo to test out distributed builds
TypeScript
1
star
23

backbone.structuredModel

A Backbone plugin to allow for Models structured with Fields.
JavaScript
1
star
24

backbone.eventsFix

Fixes the event unbinding issue in Backbone 0.5.3, where no context parameter is accepted.
JavaScript
1
star
25

Data.js

JavaScript
1
star