Babel plugin to transpile Flow code to TypeScript
npm install babel-plugin-reflow> Babel plugin to transpile Flow types to
> TypeScript with CLI wrapper.
!Tests

#### I would love to receive feedback whether this plugin worked for you :)!
Reflow enables you to migrate a whole Flow based project to TypeScript by
transpiling the Flow type annotations to equivalent TypeScript code. While this
reduces the effort to move a large code base to TypeScript drastically, it is
still very likely that you will face new type errors after the migration due to
the differences between Flow and TypeScript. See this
repository for an excellent
overview of the differences and similarities of Flow and Typescript.
Of course, I am aware that other approaches exist to translate Flow to
TypeScript. For instance, there is
Kiikurage/babel-plugin-flow-to-typescript
and
Khan/flow-to-ts.
When I started this project in the course of my master thesis in February 2019,
the development of the first plugin seemed inactive and the second one did not
exist yet. Therefore this plugin was developed to solve the given problem of the
thesis in practice.
Advantages of Reflow
- can be used either as standalone Babel plugin or through the included CLI to
transpile whole code bases
- well tested with high code coverage
- proven to work with real React based projects (two code bases with 27 and 41
kLOC respectively were successfully migrated)
- generates well formatted output based on Prettier with focus on placing
comments at the correct position (Babel fails to do so)
```
yarn add --dev babel-plugin-reflow
This package includes a small CLI wrapper for the Babel plugin to recursively
transpile whole directories or single files. Install the package as project
dependency and run npx reflow afterwards. Alternatively you might want toreflow
install Reflow globally so you can simply type :
`shell`
yarn global add babel-plugin-reflow
Usage is as follows:
`shell
$ npx reflow --help
Usage: reflow [OPTION]...
REFLOW - Flow to TypeScript converter
Options:
-v Output the version number
-d, --dry-run Perform a trial run printing to stdout instead of writing a file
-e, --exclude-dirs
-i, --include-pattern
-r, --replace Process files in-place instead of creating new TS files next to the original JS files
-D, --replace-decorators Replace class @decorators with wrapped function calls to avoid TypeScript errors (default: false)
-h, --help Output ussage information
Examples:
$ reflow --replace src/
$ reflow -d -i '/__tests__//*.{js,jsx}' src/
$ reflow -exclude-patterns '/__tests__//','fixtures/.js' src/
`
TODO
TODO
Some Flow types are not equivalently expressible in TypeScript. See the list of
unsupported Flow features below.
| Type | Flow | TypeScript |
| ----------------------- | ------------------------------ | ------------------------------------ |
| Any type | any | any |Array
| Array type | | Array |true
| Boolean literal type | | true |boolean
| Boolean type | | boolean |empty
| Empty type | | never |{\| p: number \|}
| Exact object type | | { p: number } |(string, {}) => number
| Function type | | (p1: string, p2: {}) => number |let v:
| Generic type annotation | | let v: |type Generic
| Generics | | type Generic |interface I { +p: number }
| Interface type | | interface I { readonly p: number } |type Intersection = T1 & T2
| Intersection type | | type Intersection = T1 & T2 |mixed
| Mixed type | | unknown |null
| Null literal type | | null |?number
| Nullable type (Maybe) | | number \| null \| undefined |42
| Number literal type | | 42 |number
| Number type | | number |{ [string]: number }
| Object type | | { [key: string]: number } |opaque type Opaque = number
| Opaque type | | type Opaque = number |'literal'
| String literal type | | 'literal' |string
| String type | | string |this
| This type | | this |[Date, number]
| Tuple type | | [Date, number] |type Type =
| Type alias | | type Type = |(t: T)
| Type casting | | (t as T) |import type T from './types'
| Type exports / imports | | import T from './types |typeof undefined
| Typeof type | | undefined |number \| null
| Union type | | number \| null |void
| Void type | | void |
| Utility Type | Flow | TypeScript |
| -------------------------- | --------------------- | ------------------------- |
| Call | $Call | ReturnType |Class
| Class | | typeof T |$Diff
| Difference | | Omit |$ElementType
| Element type | | T[k] |$Exact
| Exact | | T |*
| Existential type | | any |$Keys
| Keys | | keyof T |$NonMaybeType
| None maybe type | | NonNullable |$ObjMap
| Object map | | any |$ObjMapi
| Object map with key | | any |$PropertyType
| Property type | | T[k] |$ReadOnly
| ReadOnly | | Readonly |$Rest
| Rest | | Omit> |$Shape
| Shape | | Partial |$TupleMap
| Tuple map | | any |$Values
| Values | | T[keyof T] |any
| Subtype | _deprecated_ | |any
| Supertype | _deprecated_ | |
\*
| Declaration | Flow | TypeScript |
| ----------- | ------------------------------------- | -------------------------------------------------------- |
| Class | declare class C {} | declare class C {} |declare export default () => string
| Export | | const _default: () => string; export default _default; |declare function f(number): any
| Function | | declare function f(p: number): any |declare interface I {}
| Interface | | declare interface I {} |declare module 'esmodule' {}
| Module | | declare module 'esmodule' {} |declare type T = number
| Type alias | | declare type T = number |declare var v: any
| Variable | | declare var v: any |
Unsupported: CommonJS export declarations.
---
Some Flow features are not equivalently expressible in TypeScript. The Reflow
CLI will output a warning with the source code location, whenever one of the
following cases are encountered:
TypeScript intentionally doesn't support return types for constructor
functions. These will be removed by Reflow.
Flow's
existential type
has been deprecated and should be avoided. Still Reflow supports it and will
transform it to any.
- Function types with unnamed parameters
In contrast to TypeScript, parameter names can be omitted in Flow. Therefore
Reflow inserts parameter names automatically (p for a single parameter andp{i}
for multiple ones).
``
type FunctionType = ({}, Date) => string; // Flow
type FunctionType = (p1: {}, p2: Date) => string; // TypeScript
Flow allows any type for keys in index signatures, but Typescript only accepts
string or number. Reflow will add index signatures both for string andnumber
if a different type is specified in Flow.
`
// Flow
declare type KeyType;
interface I = {
[key: KeyType]: number
}
// TypeScript
interface I = {
[key: number]: number;
[key: string]: number;
}
`
Object types can be spread into other object types in Flow. Unfortunately this
syntax is not supported in TypeScript at the moment. Therefore, these
properties will be ommited in output. Please fix affected object types
manually.
`
// Flow
type ObjectWithSpread = {
prop: mixed;
...ObjectType;
};
// TypeScript
type ObjectWithSpread = {
prop: unknown;
};
`
Opaque types are not supported in TypeScript and are transformed to an
ordinary type alias.
``
opaque type T = number; // Flow
type T = number; // TypeScript
- Variance
Flow's contravariance sigil - is not expressible in Typescript and will be+
omitted. However, TypeScript does support covariance for certain types (readonly
becomes ).
`
// Flow
interface I {
+covariant: any;
-contravariant: any;
}
// TypeScript
interface I {
readonly covariant: any;
contravariant: any;
}
`
- \$Call
The $Call utility type is transformed to TypeScript'sReturnType
. Since this type only accepts the function type and not theunknown
function argument types, it is impossible to infer the return type of
polymorphic functions. TypeScript will assume an type then.
This Babel plugin enables a few other Babel plugins to support various kinds of
syntax:
- Class properties
- Decorators
- Dynamic imports
- Nullish coalescence
- Optional chaining
- React
- JSX
Clone this repository and install the project dependencies:
``
yarn install
There are various npm scripts for different tasks:
```
yarn build # Create a production build
yarn dev # Build in development mode and watch for changes
yarn format # Format the code with Prettier
yarn lint # Run ESLint
yarn test # Run fixture tests
yarn test:coverage # Run the tests with coverage report
yarn tsc # Check the types (via TypeScript)