{"_id":"@bardak-dev/js-to-ts-converter","name":"@bardak-dev/js-to-ts-converter","dist-tags":{"latest":"0.18.2"},"versions":{"0.18.2":{"name":"@bardak-dev/js-to-ts-converter","version":"0.18.2","description":"Small utility to rename .js->.ts, and convert ES6 classes to TypeScript classes by filling in property declarations. See readme for more details.","scripts":{"build":"tsc","cli":"npm run build && node dist/cli.js","test":"mocha --require ts-node/register --timeout 60000 --watch-extensions ts \"**/*.spec.ts\"","prepublishOnly":"npm test && npm run build"},"keywords":["typescript","conversion"],"homepage":"https://github.com/gregjacobs/js-to-ts-converter","repository":{"type":"git","url":"git+https://github.com/gregjacobs/js-to-ts-converter.git"},"bugs":{"url":"https://github.com/gregjacobs/js-to-ts-converter/issues"},"author":{"name":"Gregory Jacobs","email":"greg@greg-jacobs.com"},"license":"MIT","main":"dist/index.js","types":"dist/index.d.ts","bin":{"js-to-ts-converter":"dist/cli.js"},"dependencies":{"argparse":"^1.0.10","fast-glob":"^3.2.4","graphlib":"^2.1.5","lodash":"^4.17.20","resolve":"^1.8.1","trace-error":"^0.0.7","ts-morph":"^14.0.0","typescript":"^4.6.2","winston":"^3.0.0"},"devDependencies":{"@types/chai":"^4.1.4","@types/graphlib":"^2.1.4","@types/lodash":"^4.14.112","@types/mocha":"^5.2.4","@types/node":"^10.5.2","@types/winston":"^2.3.9","chai":"^4.1.2","mocha":"^5.2.0","ts-node":"^7.0.0"},"_id":"@bardak-dev/js-to-ts-converter@0.18.2","gitHead":"7444ece835eb904800bec03647c9a7af6e0e7309","_nodeVersion":"22.4.0","_npmVersion":"10.8.1","dist":{"integrity":"sha512-WLXTVA9oBRd0OjwNwIhMEajJBt+FplLD4wM+oTavLasiVIyFeNa9r5b1mA9F6aXEqILjr6HvaFBPwRFmE0DD5g==","shasum":"9843fb918078bbd2b49ebc9f6fd0995463049667","tarball":"https://registry.npmjs.org/@bardak-dev/js-to-ts-converter/-/js-to-ts-converter-0.18.2.tgz","fileCount":49,"unpackedSize":198045,"signatures":[{"keyid":"SHA256:jl3bwswu80PjjokCgh0o2w5c2U4LhQAE57gj9cz1kzA","sig":"MEUCIQCeyGIskjcCKVw6z1wDnhqPOjeMUEtxD1tGQNNsYoyy7wIgAN+EmXi7xcgsakCvd3kYtvpkq6Bw4OZwpsFcBPHp+gg="}]},"_npmUser":{"name":"bardak-dev","email":"valera.budin@gmail.com"},"directories":{},"maintainers":[{"name":"bardak-dev","email":"valera.budin@gmail.com"}],"_npmOperationalInternal":{"host":"s3://npm-registry-packages","tmp":"tmp/js-to-ts-converter_0.18.2_1721137406250_0.8881470145113142"},"_hasShrinkwrap":false}},"time":{"created":"2024-07-16T13:43:26.130Z","0.18.2":"2024-07-16T13:43:26.495Z","modified":"2024-07-16T13:43:26.771Z"},"maintainers":[{"name":"bardak-dev","email":"valera.budin@gmail.com"}],"description":"Small utility to rename .js->.ts, and convert ES6 classes to TypeScript classes by filling in property declarations. See readme for more details.","homepage":"https://github.com/gregjacobs/js-to-ts-converter","keywords":["typescript","conversion"],"repository":{"type":"git","url":"git+https://github.com/gregjacobs/js-to-ts-converter.git"},"author":{"name":"Gregory Jacobs","email":"greg@greg-jacobs.com"},"bugs":{"url":"https://github.com/gregjacobs/js-to-ts-converter/issues"},"license":"MIT","readme":"# js-to-ts-converter\n\nSmall utility that I wrote to script converting a JS codebase to TypeScript,\nwhile trying to solve some of the common TypeScript errors that will be received\nupon such a conversion.\n\nThe utility performs the following transformations:\n\n1. Renames `.js` files to `.ts`\n2. Adds property declarations to ES6 classes so that they are compilable by the\n   TypeScript compiler (see below).\n3. Any function calls that provide fewer arguments than the declared parameters\n   in the function will cause the remaining parameters to be marked as optional\n   for that function. This solves TS errors like \"Expected 3 arguments, but \n   got 2\"\n   \nNote: because this utility utilizes the TypeScript Language Service to perform\nthe look-ups for #3, it may take a long time to run. For a small project, expect\na few minutes. For a larger project, it could take tens of minutes. Still much \nbetter than the days/weeks it could take to fix an entire codebase by hand :)\n \n\nFor #2 above, the utility basically looks at any `this` property accessed by a \nJS class, and fills in the appropriate TypeScript property declarations. Take \nthis `.js` input source file as an example:\n\n```js\nclass Super {\n\tsomeMethod() {\n\t\tthis.superProp = 1;\n\t}\n}\n\nclass Sub extends Super {\n\tsomeMethod() {\n\t\tthis.superProp = 2;\n\t\tthis.subProp = 2;\n\t}\n}\n```\n\n\nThe above JS classes are replaced with the following TS classes:\n\n```ts\nclass Super {\n    public superProp: any;   // <-- added\n\n    someMethod() {\n        this.superProp = 1;\n    }\n}\n\n\nclass Sub extends Super {\n    public subProp: any;    // <-- added\n\n    someMethod() {\n        this.superProp = 2;\n        this.subProp = 2;\n    }\n}\n```\n\nNote: properties used when `this` is assigned to another variable are also \nfound for purposes of creating property declarations. Example:\n\n```js\nclass MyClass {\n    myMethod() {\n        var that = this;\n        \n        that.something;  // <-- 'something' parsed as a class property\n    }\n}\n```\n\nFor #3 above, parameters are marked as 'optional' when there are callers that\ndon't provide all of them. For example, the following JavaScript:\n\n```js\nfunction myFunction( arg1, arg2 ) {\n\t// ...\n}\n\nmyFunction( 1 );  // only provide arg1\n```\n\nWill be transformed to the following TypeScript:\n\n```ts\nfunction myFunction( arg1, arg2? ) {  // <-- arg2 marked as optional\n\t// ...\n}\n\nmyFunction( 1 );\n```\n\n## Goal\n\nThe goal of this utility is to simply make the `.js` code compilable under the\nTypeScript compiler, so simply adding the property declarations typed as `any` \nwas the quickest option there. The utility may look at property initializers in \nthe future to determine a better type.\n\nIf you have other types of compiler errors that you think might be able to be \ntransformed by this utility, please feel free to raise an issue (or pull\nrequest!)\n\nHopefully you only need to use this utility once, but if it saved you time, \nplease star it so that I know it helped you out :)\n\n\n## Fair Warning\n\nThis utility makes modifications to the directory that you pass it. Make sure\nyou are in a clean git (or other VCS) state before running it in case you need\nto revert!\n\n\n## Running the Utility from the CLI\n\n```\nnpx js-to-ts-converter ./path/to/js/files\n```\n\nIf you would prefer to install the CLI globally, do this:\n\n```\nnpm install --global js-to-ts-converter\n\njs-to-ts-converter ./path/to/js/files\n```\n\n\n## Running the Utility from Node\n\nTypeScript: \n\n```ts\nimport { convertJsToTs, convertJsToTsSync } from 'js-to-ts-converter';\n\n\n// Async\nconvertJsToTs( 'path/to/js/files' ).then( \n    () => console.log( 'Done!' ),\n    ( err ) => console.log( 'Error: ', err );\n); \n\n\n// Sync\nconvertJsToTsSync( 'path/to/js/files' );\nconsole.log( 'Done!' );\n```\n\nJavaScript:\n\n```js\nconst { convertJsToTs, convertJsToTsSync } = require( 'js-to-ts-converter' );\n\n\n// Async\nconvertJsToTs( 'path/to/js/files' ).then( \n    () => console.log( 'Done!' ),\n    ( err ) => console.log( 'Error: ', err );\n); \n\n\n// Sync\nconvertJsToTsSync( 'path/to/js/files' );\nconsole.log( 'Done!' );\n```\n\n## Developing\n\nMake sure you have [Node.js](https://nodejs.org) installed. \n\nClone the git repo: \n\n```\ngit clone https://github.com/gregjacobs/js-to-ts-converter.git\n\ncd js-to-ts-converter\n```\n\nInstall dependencies:\n\n```\nnpm install\n```\n\nRun Tests:\n\n```\nnpm test\n```\n","readmeFilename":"README.md"}