fable-compiler/ts2fable

Name: ts2fable

Owner: Fable

Description: Parser of Typescript declaration files

Created: 2017-03-25 23:21:25.0

Updated: 2018-04-01 10:30:42.0

Pushed: 2018-03-25 19:33:35.0

Homepage: null

Size: 1104

Language: F#

GitHub Committers

UserMost Recent Commit# Commits

Other Committers

UserEmailMost Recent Commit# Commits

README

ts2fable npm version

Fable parser for TypeScript declaration files.

Usage

Install it with yarn or npm. With yarn it is:

 global add ts2fable

With npm it is:

install -g ts2fable

Run the ts2fable command on a TypeScript file and also specify the F# output file. The F# namespace in taken from the output filename. In this example, it is Yargs.

 add @types/yargs --dev
able node_modules/@types/yargs/index.d.ts src/Yargs.fs

You can also use --export(or -e) option to collect from multiple tsfiles

In below sample: All the related ts files in npm packages uifabric and office-ui-fabric-react will be compiled to OfficeReact.fs as a bundle

able node_modules/office-ui-fabric-react/lib/index.d.ts test-compile/OfficeReact.fs -e uifabric office-ui-fabric-react

You can find more information about how to interact with JavaScript from F# here. Please note the parser is not perfect and some tweaking by hand may be needed. Please submit bugs as issues on GitHub.

Contributing

Succesfull builds on the master branch are uploaded and tagged as next. You can help us test these builds by installing them with:

 global add ts2fable@next
Running code from source

Windows:

Unix:

Common to all OS

Sample Test:

 "duplicated variable exports" <| fun _ ->
let tsPaths = ["node_modules/reactxp/dist/web/ReactXP.d.ts"]
let fsPath = "test-compile/ReactXP.fs"
testFsFiles tsPaths fsPath  <| fun fsFiles ->
    fsFiles
    |> getTopVarialbles
    |> List.countBy(fun vb -> vb.Name)
    |> List.forall(fun (_,l) -> l = 1)
    |> equal true
Conventions

Some JavaScript/TypeScript features have no direct translation to F#. Here is a list of common workarounds adopted by the parser to solve these problems:

 CanvasRenderingContext2D =
abstract fillStyle: U3<string, CanvasGradient, CanvasPattern> with get, set

ctx: CanvasRenderingContext2D = failwith "dummy"
fillStyle <- U3.Case1 "#FF0000"
 CanvasRenderingContext2DType =
abstract prototype: CanvasRenderingContext2D with get, set
[<Emit("new $0($1...)")>] abstract Create: unit -> CanvasRenderingContext2D
 Express =
inherit Application
abstract version: string with get, set
abstract application: obj with get, set
[<Emit("$0($1...)")>] abstract Invoke: unit -> Application

This work is supported by the National Institutes of Health's National Center for Advancing Translational Sciences, Grant Number U24TR002306. This work is solely the responsibility of the creators and does not necessarily represent the official views of the National Institutes of Health.