Transform Flow files to TypeScript.
# Using Yarn:
yarn add @esmbly/transformer-flow
# Or, using NPM:
npm install @esmbly/transformer-flow --save
Check out Using the Flow transformer guide for a step-by-step guide on how to get started with @esmbly/transformer-flow
and the command-line interface.
Try it out in the Esmbly version of WebAssembly Studio!
The @esmbly/transformer-flow
transforms Flow files to TypeScript. In order to use it, make sure you have @esmbly/cli
installed .
The following Esmbly configuration will transform all JavaScript files located in the src
directory and output TypeScript files to the dist
directory.
// esmbly.config.js
const Flow = require('@esmbly/transformer-flow');
module.exports = {
input: ['./src/**/*.js'],
transformers: [
Flow.createTransformer(),
],
output: [
{
format: '.ts',
outDir: 'dist',
rootDir: 'src',
},
],
};
The createTransformer()
method accepts an optional configuration object (see the FlowTransformerOptions interface). The following options are possible.
Option | Description | Type | Default |
---|---|---|---|
removeFlowFlags (optional) | Whether or not to remove any // @flow comments |
boolean |
true |
customRules (optional) | An object containing any custom rules which should be applied (existing rules can be overridden). Check out the custom-rule example for further details. | CustomRules |
- Yarn/lockfile: Transforming a subset of yarn from Flow to TypeScript.
- Add: Transforming a simple Flow program to WebAssembly by chaining
@esmbly/transformer-flow
and@esmbly/transformer-wasm
. - Pad: Transforming a string pad program to WebAssembly by chaining
@esmbly/transformer-flow
and@esmbly/transformer-wasm
. - Custom Rule: Extending
@esmbly/transformer-flow
with a custom rule. - Browser Example: Using esmbly and
@esmbly/transformer-flow
in the browser using Webpack.
Supported? | Flow | TypeScript | |
---|---|---|---|
✅ | Primitive Types | boolean | number | string | null | undefined |
boolean | number | string | null | undefined |
✅ | Void | void |
undefined |
✅ | Literal Types | type Two = 2; |
type Two = 2; |
✅ | Mixed Types | mixed |
unknown |
✅ | Any Types | any |
any |
✅ | Maybe Types | ?type |
type | null | undefined |
✅ | Function Types | (str: string) => boolean |
(str: string) => boolean |
❌ | Function Shorthand | (A, B) => C |
(a: A, b: B) => C |
❌ | Predicate Functions | (a: A, b: B) => C %checks |
(a: A, b: B) => C |
✅ | Object Types | { foo: boolean } |
{ foo: boolean } |
✅ | Exact object types | {| a: A |} |
{ a: A } |
✅ | Indexers | { [A]: B } |
{ [a: A]: B } |
✅ | Array Types | Array<string> , string[] |
Array<string> , string[] |
✅ | Tuple Types | type A = [number, boolean] |
type A = [number, boolean] |
✅ | Class Types | ||
✅ | Opaque types | opaque type A = B |
type A = B (not expressible) |
✅ | Interface Types | ||
✅ | Variance | interface A { +b: B, -c: C } |
interface A { readonly b: B, c: C } |
✅ | Generic Types | ||
❌ | Bounds | <A: string> |
<A extends string> |
✅ | Union Types | number | boolean |
number | boolean |
❓ | Disjoint unions | ||
✅ | Intersection Types | A & B |
A & B |
❓ | Typeof types | ||
✅ | Typeof undefined | typeof undefined |
undefined |
✅ | Type Casting | (a: A) |
(a as A) |
✅ | Import default type | import type A from './b' |
import A from './b' |
✅ | Import named type | import type { A } from './b' |
import { A } from './b' |
Done? | Flow | TypeScript | |
---|---|---|---|
✅ | Keys | $Keys<A> |
keyof A |
❌ | Values | $Values<A> |
A[keyof A] |
✅ | ReadOnly | $ReadOnly<A> |
Readonly<A> |
✅ | ReadOnlyArray | $ReadOnlyArray<A> |
ReadonlyArray<A> |
✅ | Exact | $Exact<A> |
A |
❌ | Difference | $Diff<A, B> |
? |
❌ | Rest | $Rest<A, B> |
? |
❌ | Property type | $PropertyType<T, k> |
T[k] |
❌ | Element type | $ElementType<T, K> |
T[k] |
❌ | Element type | $NonMaybeType<T> |
NonNullable<T> (strictNullChecks) |
❌ | ObjMap | $ObjMap<T, F> |
? |
❌ | ObjMapi | $ObjMapi<T, F> |
? |
❌ | TupleMap | $TupleMap<T, F> |
? |
❌ | Call | $Call<F> |
ReturnType |
❌ | Class | Class<A> |
typeof A |
❌ | Shape | Shape<T> |
Partial<T> |
❌ | Supertype | $Supertype<A> |
any (deprecated) |
❌ | Subtype | $Subtype<A> |
B extends A (deprecated) |
❌ | Existential type | * |
any (deprecated) |
✅ Done
❌ Not implemented yet
❓ Status not known
All types of contributions are very much welcome. Check out our Contributing Guide for instructions on how to get started.
@esmbly/transformer-flow
is a customization/extension of the flow-to-typescript project ( ❤️) which is released under the following MIT license.
Copyright 2017 Boris Cherny <boris@performancejs.com>
Permission is hereby granted, free of charge, to any person obtaining a copy of this software and associated documentation files (the "Software"), to deal in the Software without restriction, including without limitation the rights to use, copy, modify, merge, publish, distribute, sublicense, and/or sell copies of the Software, and to permit persons to whom the Software is furnished to do so, subject to the following conditions:
The above copyright notice and this permission notice shall be included in all copies or substantial portions of the Software.
THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.