TS-Migrate是Airbnb(爱彼迎)公司开源的前端工具,可将 JavaScript 代码快速迁移为 TypeScript 代码,节省前端工程师的时间成本,统一开发环境。遵守MIT开源协议。
TS-Migrate生成的代码仍然需要后续操作来提高安全性。但比从头开始重新写代码要好得多。TS-Migrate被设计为一组插件,因此它可以针对不同的用例进行很好的自定义。可以添加更多插件来解决诸如类型质量改进或与库相关的事件(如Reaction中的属性类型)等问题。用户可以根据自己的需要添加具有不同插件集的自定义配置。这个工具并不能保证实现完全没有错误的转换,但在实际使用过程中,对于一个超过50000行代码、1000个文件的项目,从JavaScript转换到TypeScript使用这个工具基本只需1天。
源码:https://github.com/airbnb/ts-migrate
-----------------------------------------------------
ts-migrate
ts-migrate is a tool for migrating frontend application to TypeScript. Run npx ts-migrate <folder>
to convert your frontend application to TypeScript.
ts-migrate is designed around Airbnb projects. Use at your own risk.
Install
Install ts-migrate using npm:
npm install --save-dev ts-migrate
Or yarn:
yarn add --dev ts-migrate
Usage
Migrate an entire project like this:
npx -p ts-migrate -c "ts-migrate-full <folder>"
Please note that it may take a long time to do a full migration. You can also migrate individual parts of a project by specifying a subset of sources:
npx ts-migrate-full <folder> / # specify the project root, and
--sources="relative/path/to/subset/**/*" / # list the subset to migrate,
--sources="node_modules/**/*.d.ts" # including any global types that the
# migrator may need to know about.
Or, you can run individual CLI commands:
$ npx ts-migrate -- --help
npm run ts-migrate -- <command> [options]
Commands:
npm run ts-migrate -- init <folder> Initialize tsconfig.json file in <folder>
npm run ts-migrate -- rename <folder> *Rename files in folder from JS/JSX to TS/TSX
npm run ts-migrate -- migrate <folder> *Fix TypeScript errors, using codemods
npm run ts-migrate -- reignore <folder> Re-run ts-ignore on a project
* These commands can be passed a --sources (or -s) flag. This flag accepts a relative
path to a subset of your project as a string (glob patterns are allowed). When this flag
is used, ts-migrate ignores your project's default source files in favor of the ones
you've listed. It is effectively the same as replacing your tsconfig.json's `include`
property with the provided sources. The flag can be passed multiple times.
Options:
-h, -- help Show help
-i, -- init Initialize TypeScript (tsconfig.json) in <folder>
-m, -- migrate Fix TypeScript errors, using codemods
-rn, -- rename Rename files in <folder> from JS/JSX to TS/TSX
-ri, -- reignore Re-run ts-ignore on a project
Examples:
npm run ts-migrate -- --help Show help
npm run ts-migrate -- init frontend/foo Create tsconfig.json file at frontend/foo/tsconfig.json
npm run ts-migrate -- rename frontend/foo Rename files in frontend/foo from JS/JSX to TS/TSX
from
https://www.npmjs.com/package/ts-migrate