Movatterモバイル変換


[0]ホーム

URL:


Skip to content

Navigation Menu

Sign in
Appearance settings

Search code, repositories, users, issues, pull requests...

Provide feedback

We read every piece of feedback, and take your input very seriously.

Saved searches

Use saved searches to filter your results more quickly

Sign up
Appearance settings

Convert a Flow typed codebase to TypeScript (Babel plugin)

License

NotificationsYou must be signed in to change notification settings

grubersjoe/reflow

Repository files navigation

Babel plugin to transpileFlow types toTypeScript with CLI wrapper.

TestsCoverage

I would love to receive feedback whether this plugin worked for you :)!

Reflow enables you to migrate a whole Flow based project to TypeScript bytranspiling the Flow type annotations to equivalent TypeScript code. While thisreduces the effort to move a large code base to TypeScript drastically, it isstill very likely that you will face new type errors after the migration due tothe differences between Flow and TypeScript. See thisrepository for an excellentoverview of the differences and similarities of Flow and Typescript.

Why another plugin?

Of course, I am aware that other approaches exist to translate Flow toTypeScript. For instance, there isKiikurage/babel-plugin-flow-to-typescriptandKhan/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 notexist yet. Therefore this plugin was developed to solve the given problem of thethesis in practice.

Advantages of Reflow

  • can be used either as standalone Babel plugin or through the included CLI totranspile whole code bases
  • well tested with high code coverage
  • proven to work with real React based projects (two code bases with 27 and 41kLOC respectively were successfully migrated)
  • generates well formatted output based on Prettier with focus on placingcomments at the correct position (Babel fails to do so)

Installation

yarn add --dev babel-plugin-reflow

Usage

CLI

This package includes a small CLI wrapper for the Babel plugin to recursivelytranspile whole directories or single files. Install the package as projectdependency and runnpx reflow afterwards. Alternatively you might want toinstall Reflow globally so you can simply typereflow:

yarn global add babel-plugin-reflow

Usage is as follows:

$ npx reflow --helpUsage: reflow [OPTION]...<FILES OR DIRECTORIES ...>REFLOW - Flow to TypeScript converterOptions:  -v                                Output the version number  -d, --dry-run                     Perform a trial run printing to stdout instead of writing a file  -e, --exclude-dirs<pattern ...>  Comma-separated list of directories to recursively exclude (default: ["node_modules"])  -i, --include-pattern<pattern>   Set the glob patternfor input files (default:"**/*.{js,jsx}")  -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 wrappedfunctioncalls to avoid TypeScript errors (default: false)  -h, --help                        Output ussage informationExamples:  $ reflow --replace src/  $ reflow -d -i'**/__tests__/**/*.{js,jsx}' src/  $ reflow -exclude-patterns'**/__tests__/**/*','fixtures/*.js' src/

Programmatically

TODO

As Babel plugin

TODO

Transformations

Base types

Some Flow types are not equivalently expressible in TypeScript. See the list ofunsupported Flow features below.

TypeFlowTypeScript
Any typeanyany
Array typeArray<number>Array<number>
Boolean literal typetruetrue
Boolean typebooleanboolean
Empty typeemptynever
Exact object type{| p: number |}{ p: number }
Function type(string, {}) => number(p1: string, p2: {}) => number
Generic type annotationlet v: <FlowType>let v: <TSType>
Genericstype Generic<T: Super> = Ttype Generic<T extends Super> = T
Interface typeinterface I { +p: number }interface I { readonly p: number }
Intersection typetype Intersection = T1 & T2type Intersection = T1 & T2
Mixed typemixedunknown
Null literal typenullnull
Nullable type (Maybe)?numbernumber | null | undefined
Number literal type4242
Number typenumbernumber
Object type{ [string]: number }{ [key: string]: number }
Opaque typeopaque type Opaque = numbertype Opaque = number
String literal type'literal''literal'
String typestringstring
This typethisthis
Tuple type[Date, number][Date, number]
Type aliastype Type = <FlowType>type Type = <TSType>
Type casting(t: T)(t as T)
Type exports / importsimport type T from './types'import T from './types
Typeof typetypeof undefinedundefined
Union typenumber | nullnumber | null
Void typevoidvoid

Utility types

Utility TypeFlowTypeScript
Call$Call<F, T...>ReturnType<F>
ClassClass<T>typeof T
Difference$Diff<A, B>Omit<A, keyof B>
Element type$ElementType<T, K>T[k]
Exact$Exact<T>T
Existential type*any
Keys$Keys<T>keyof T
None maybe type$NonMaybeType<T>NonNullable<T>
Object map$ObjMap<T, F>any
Object map with key$ObjMapi<T, F>any
Property type$PropertyType<T, k>T[k]
ReadOnly$ReadOnly<T>Readonly<T>
Rest$Rest<A, B>Omit<A, Union<keyof B>>
Shape$Shape<T>Partial<T>
Tuple map$TupleMap<T, F>any
Values$Values<T>T[keyof T]
Subtypedeprecatedany
Supertypedeprecatedany

*

Declarations

DeclarationFlowTypeScript
Classdeclare class C {}declare class C {}
Exportdeclare export default () => stringconst _default: () => string; export default _default;
Functiondeclare function f(number): anydeclare function f(p: number): any
Interfacedeclare interface I {}declare interface I {}
Moduledeclare module 'esmodule' {}declare module 'esmodule' {}
Type aliasdeclare type T = numberdeclare type T = number
Variabledeclare var v: anydeclare var v: any

Unsupported: CommonJS export declarations.


Unsupported Flow features / syntax

Some Flow features are not equivalently expressible in TypeScript. The ReflowCLI will output a warning with the source code location, whenever one of thefollowing cases are encountered:

  • Constructor return types

    TypeScript intentionally doesn't support return types for constructorfunctions. These will be removed by Reflow.

  • Existential Type

    Flow'sexistential typehas been deprecated and should be avoided. Still Reflow supports it and willtransform it toany.

  • Function types with unnamed parameters

    In contrast to TypeScript, parameter names can be omitted in Flow. ThereforeReflow inserts parameter names automatically (p for a single parameter andp{i} for multiple ones).

    type FunctionType = ({}, Date) => string;             // Flowtype FunctionType = (p1: {}, p2: Date) => string;    // TypeScript
  • Index signatures

    Flow allows any type for keys in index signatures, but Typescript only acceptsstring ornumber. Reflow will add index signatures both forstring andnumber if a different type is specified in Flow.

    // Flowdeclare type KeyType;interface I = {  [key: KeyType]: number}// TypeScriptinterface I = {  [key: number]: number;  [key: string]: number;}
  • Object type spread

    Object types can be spread into other object types in Flow. Unfortunately thissyntax is not supported in TypeScript at the moment. Therefore, theseproperties will be ommited in output. Please fix affected object typesmanually.

    // Flowtype ObjectWithSpread = {  prop: mixed;  ...ObjectType;};// TypeScripttype ObjectWithSpread = {  prop: unknown;};
  • Opaque Type

    Opaque types are not supported in TypeScript and are transformed to anordinary type alias.

    opaque type T = number;  // Flowtype T = number;         // TypeScript
  • Variance

    Flow's contravariance sigil- is not expressible in Typescript and will beomitted. However, TypeScript does support covariance for certain types (+becomesreadonly).

    // Flowinterface I {  +covariant: any;  -contravariant: any;}// TypeScriptinterface I {  readonly covariant: any;  contravariant: any;}
  • $Call<F, T...>

    The$Call<F, T...> utility type is transformed to TypeScript'sReturnType<F>. Since this type only accepts the function type and not thefunction argument types, it is impossible to infer the return type ofpolymorphic functions. TypeScript will assume anunknown type then.

Supported syntax

This Babel plugin enables a few other Babel plugins to support various kinds ofsyntax:

Development

Clone this repository and install the project dependencies:

yarn install

There are various npm scripts for different tasks:

yarn build          # Create a production buildyarn dev            # Build in development mode and watch for changesyarn format         # Format the code with Prettieryarn lint           # Run ESLintyarn test           # Run fixture testsyarn test:coverage  # Run the tests with coverage reportyarn tsc            # Check the types (via TypeScript)

About

Convert a Flow typed codebase to TypeScript (Babel plugin)

Topics

Resources

License

Stars

Watchers

Forks

Contributors3

  •  
  •  
  •  

[8]ページ先頭

©2009-2025 Movatter.jp