首页 > 解决方案 > 是否有脚本或工具可以帮助将 Flow 自动迁移到 TypeScript?

问题描述

我有一个使用 Flow 的旧 react-native 模块。有几千行代码。现在,我想将这些代码迁移到使用 TypeScript 的新项目中。我正在寻找一种自动工具来帮助我解决繁琐的类型转换工作。如果它能解决大约 90% 的问题,我将不胜感激。例如,

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

标签: typescriptreact-nativeflowtypemigrate

解决方案


推荐阅读