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

Generate flowtype definition files from TypeScript

License

NotificationsYou must be signed in to change notification settings

joarwilk/flowgen

 
 

Repository files navigation

The state of the converter

It's surprisingly robust and non-lossy as it stands right now, in big part thanks to how similar flow and typescript definition files are. Please see the output inthis flow-typed PR for the state of the output.

Supported?SyntaxTypeScriptFlow
Void typevoidvoid
Undefined typeundefinedvoid
Unknown typeunknownmixed
Symbol typesymbolSymbol
Unique symbol typeunique symbolSymbol
Object typeobject{[key: string]: any}
Never typeneverempty
Varianceinterface A { readonly b: B, c: C }interface A { +b: B, c: C }
Functions(a: A, b: B) => C(a: A, b: B) => C
Indexers{[k: string]: string}{[k: string]: string}
This type(this: X, a: A, b: B) => C(a: A, b: B) => C
Type guards(a: X) => a is A(a: X) => boolean
Type parameter boundsfunction f<A extends string>(a:A){}function f<A: string>(a:A){}
keyof Xkeyof X$Keys<X>
X[keyof X]X[keyof X]$ElementType<X, $Keys<X>>
PartialPartial<X>$Rest<X, {}>
ReadonlyReadonly<X>$ReadOnly<X>
ReadonlyArrayReadonlyArray<X>$ReadOnlyArray<X>
ReadonlySetReadonlySet<X>$ReadOnlySet<X>
ReadonlyMapReadonlyMap<X, Y>$ReadOnlyMap<X, Y>
RecordRecord<K, T>{ [key: K]: T }
PickPick<T, K>
ExcludeExclude<T, U>
ExtractExtract<T, U>
NonNullableNonNullable<X>$NonMaybeType<X>
ReturnTypeReturnType<F>$Call<<R>((...args: any[]) => R) => R, F>
InstanceTypeInstanceType<X>
RequiredRequired<X>
ThisTypeThisType<X>
T['string']T['string']$PropertyType<T, k>
T[k]T[k]$ElementType<T, k>
Mapped types{[K in keyof Obj]: Obj[K]}$ObjMapi<Obj, <K>(K) => $ElementType<Obj, K>>
Conditional typesA extends B ? C : Dany
typeof operatortypeof footypeof foo
Tuple type[number, string][number, string]
Type aliastype A = stringtype A = string
type/typeof importimport A from 'module'import type A from 'module'

Usage

Install usingnpm i flowgen --save

import{compiler}from'flowgen';// To compile a d.ts fileconstflowdef=compiler.compileDefinitionFile(filename);// To compile a stringconstflowdef=compiler.compileDefinitionString(str);// To compile a typescript test file to JavaScript// esTarget = ES5/ES6 etcconsttestCase=compiler.compileTest(path,esTarget)

Recommended second step:

import{beautify}from'flowgen';// Make the definition human readableconstreadableDef=beautify(generatedFlowdef);

CLI

Standard usage (will produceexport.flow.js):

npm i -g flowgenflowgen lodash.d.ts

Options

-o / --output-file [outputFile]: Specifies the filename of the exported file, defaults to export.flow.js

Flags for specific cases

--flow-typed-format: Format output so it fits in the flow-typed repo--compile-tests: Compile any sibling <filename>-tests.ts files found--no-inexact: Do not mark object types as inexact (using `...`)--no-module-exports: Convert `export = Type` only to default export, instead of `declare module.exports: Type`--interface-records: Convert TypeScript interfaces to Exact Objects--no-jsdoc: Ignore TypeScript JSDoc--add-flow-header: Add `// @flow` to generated files. Should be used for libs.

The difficult parts

Namespaces

Namespaces have been a big headache. What it does right now is that it splits any namespace out into prefixed global scope declarations instead. It works OK, but its not pretty and there's some drawbacks to it.

External library imports

Definitions in TS and flow are often quite different, and imported types from other libraries don't usually havea one-to-one mapping. Common cases areReact.ReactElement,React.CSSPropsetc.This might require manual processing, or we add a set of hardcoded mutations that handle common cases.

Odd TS conventions

Lodash has been one of the reference libraries i've worked with when creating theconverter. The definition is mostly just a series of interfaces with the same name being re-declared over and over again for each function, which doesn't translate to flow at all. There's multiple ways of solving this but I don't have a great solution for it in place yet.

Sample of finding all typescript definition files and generate flow file with shell script

If your typescript definition files are built inlib add below shell script and run it.

foriin$(find lib -type f -name"*.d.ts");do sh -c"flowgen$i -o${i%.*.*}.js.flow";done;

So if you have definition files in different dir, you can renamelib and run the script.

Here’s an example of the above as an npm script inpackage.json that excludes any typescript definition files found insidenode_modules:

"scripts": {"build:flowtypes":"find . -type f -not -path './node_modules/*' -name '*.d.ts' -exec sh -c 'yarn flowgen --add-flow-header $1 -o ${1%.*.*}.js.flow' _ '{}'\\;"  }

You can then have abuild script that generates flow types along the lines oftsc --build && npm run build:flowtypes.

Contributing

All help is appreciated. Pleasetweet at me if you want some help getting started, or just want to discuss ideas on how to solve the trickier parts.

Distribution

  • git pull origin master
  • yarn compile
  • Change the version inpackage.json
  • git add .
  • `git commit -m "New release"
  • npm publish
  • git push

About

Generate flowtype definition files from TypeScript

Topics

Resources

License

Stars

Watchers

Forks

Releases

No releases published

Packages

No packages published

Contributors34


[8]ページ先頭

©2009-2025 Movatter.jp