typescript - 是否有脚本或工具可以帮助将 Flow 自动迁移到 TypeScript?
问题描述
我有一个使用 Flow 的旧 react-native 模块。有几千行代码。现在,我想将这些代码迁移到使用 TypeScript 的新项目中。我正在寻找一种自动工具来帮助我解决繁琐的类型转换工作。如果它能解决大约 90% 的问题,我将不胜感激。例如,
- 将所有
.js
文件更改为 'ts
或tsx
文件,例如:
for f in `find . -type f -name '*.js'`;
do
git mv -- "$f" "${f%.js}.ts"
done
- 转换类型
// Flow
import type { Type1, Type2 } from ./dir/to/path
// Typescript
import { Type1, Type2 } from ./dir/to/path
// Flow
type Date = {
toString: () => string,
setTime: (time: number) => number
}
// TypeScript
interface Date {
toString(): string;
setTime(time: number): number;
}
// Flow
value: ?string
// TypeScript
value: string | null
有什么建议么?
最后,我使用了 flow-to-ts
yarn global add @khanacademy/flow-to-ts
flow-to-ts --write --delete-source ${myProjectPath}/src/**/**.js
还有其他选择:
Usage: flow-to-ts [options]
Options:
-V, --version output the version number
--inline-utility-types inline utility types when possible, defaults to 'false'
--prettier use prettier for formatting
--semi add semi-colons, defaults to 'false' (depends on --prettier)
--single-quote use single quotes instead of double quotes, defaults to 'false' (depends on --prettier)
--tab-width [width] size of tabs (depends on --prettier) (default: 4)
--trailing-comma [all|es5|none] where to put trailing commas (depends on --prettier) (default: "all")
--bracket-spacing put spaces between braces and contents, defaults to 'false' (depends on --prettier)
--arrow-parens [avoid|always] arrow function param list parens (depends on --prettier) (default: "avoid")
--print-width [width] line width (depends on --prettier) (default: 80)
--write write output to disk instead of STDOUT
--delete-source delete the source file
-h, --help output usage information
解决方案
推荐阅读
- c# - 尝试从 gridview 更新时出现死锁错误
- angular - angular ng serve 命令抛出错误:发生未处理的异常:项目不存在
- linux - 尝试运行 bash 脚本时出现“找不到命令”
- python-3.x - 如何使用 pycharm 在生产模式下发布应用程序?
- automation - Cypress 获取属性值并存储在变量中
- python-3.x - 将 UTC 日期时间列转换为 IST 数据时间
- bash - 使用 bash 命令一次下载所有 gitlab 项目
- laravel - npm 模块构建失败
- elasticsearch - 从弹性搜索中获取最后 2 个条目的平均值
- javascript - Redux 错误操作必须是普通对象。使用自定义中间件进行异步操作