npm package discovery and stats viewer.

Discover Tips

  • General search

    [free text search, go nuts!]

  • Package details

    pkg:[package-name]

  • User packages

    @[username]

Sponsor

Optimize Toolset

I’ve always been into building performant and accessible sites, but lately I’ve been taking it extremely seriously. So much so that I’ve been building a tool to help me optimize and monitor the sites that I build to make sure that I’m making an attempt to offer the best experience to those who visit them. If you’re into performant, accessible and SEO friendly sites, you might like it too! You can check it out at Optimize Toolset.

About

Hi, 👋, I’m Ryan Hefner  and I built this site for me, and you! The goal of this site was to provide an easy way for me to check the stats on my npm packages, both for prioritizing issues and updates, and to give me a little kick in the pants to keep up on stuff.

As I was building it, I realized that I was actually using the tool to build the tool, and figured I might as well put this out there and hopefully others will find it to be a fast and useful way to search and browse npm packages as I have.

If you’re interested in other things I’m working on, follow me on Twitter or check out the open source projects I’ve been publishing on GitHub.

I am also working on a Twitter bot for this site to tweet the most popular, newest, random packages from npm. Please follow that account now and it will start sending out packages soon–ish.

Open Software & Tools

This site wouldn’t be possible without the immense generosity and tireless efforts from the people who make contributions to the world and share their work via open source initiatives. Thank you 🙏

© 2024 – Pkg Stats / Ryan Hefner

js-to-ts-converter

v0.18.2

Published

Small utility to rename .js->.ts, and convert ES6 classes to TypeScript classes by filling in property declarations. See readme for more details.

Downloads

4,019

Readme

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