Package Exports
- js-to-ts-converter
This package does not declare an exports field, so the exports above have been automatically detected and optimized by JSPM instead. If any package subpath is missing, it is recommended to post an issue to the original package (js-to-ts-converter) to support the "exports" field. If that is not possible, create a JSPM override to customize the exports field for this package.
Readme
js-to-ts-converter
Small utility that I wrote to script converting a JS codebase which contains ES6 classes into TypeScript classes.
The utility does two things:
- Renames
.jsfiles to.ts - Adds property declarations to ES6 classes so that they are compilable by the TypeScript compiler (see below).
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;
}
}The goal of this utility was 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.
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
npm install --global js-to-ts-converter
js-to-ts-converter path/to/js/filesRunning 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-converterInstall dependencies:
npm installRun Tests:
npm test