• Stars
    star
    223
  • Rank 178,458 (Top 4 %)
  • Language
    F#
  • License
    Apache License 2.0
  • Created over 7 years ago
  • Updated 6 months ago

Reviews

There are no reviews yet. Be the first to send feedback to the community and the maintainers!

Repository Details

Parser of Typescript declaration files

ts2fable npm version

Fable parser for TypeScript declaration files.

Usage

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

yarn global add ts2fable

With npm it is:

npm 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.

yarn add @types/yargs --dev
ts2fable 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

ts2fable 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.

Online Version

You can also try an in-browser version here

The online version will be updated automatically when commits is merged

Contributing

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

yarn global add ts2fable@next

or build directly from source:

  • git clone https://github.com/fable-compiler/ts2fable
  • cd ts2fable
  • Compile:
    • Windows: ./fake.cmd build
    • Linux: ./fake.sh build
  • Run:
    • node -require esm ./build/cli/ts2fable.js Path/to/Declaration.d.ts Path/to/Output.fs
    • or: npm ts2fable Path/to/Declaration.d.ts Path/to/Output.fs

-require esm is needed, because fable outputs code with ES modules.
If you want to run ts2fable directly without esm (ECMAScript module loader):

  • build: ./fake.cmd build -t Cli.BuildRelease
  • call: node ./dist/ts2fable.js ...

Test Suites

  • ./test: Mocha tests
    • Run: ./fake.cmd build -t RunTest
    • Watch: ./fake.cmd build -t WatchAndRunTest
    • functionTests.fs: Test ts2fable functions
    • fsFileTests.fs: Test file translation using small snippets and compare output against expected results or content
  • ./test-compile: Translate actual TypeScript declaration files with ts2fable into F#. Then compile with F# compiler to ensure they are valid F#.
    • Run: ./fake.cmd build -t BuildTestCompile
    • Setup for translation of .ts files: ./build.fsx > Target RunCliOnTestCompile
    • Setup for .NET compilation: ./test-compile/test-compile.fsproj

Run both test suites: ./fake.cmd build -t CliTest

Debug Test:

  • In VS Code: Ctrl+Shift+P > Run Task > WatchTest
  • Add your test to ./test/fsFileTests.fs and prefix with mocha only (See below sample)

Sample Test:

only "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
        |> getTopVariables
        |> List.countBy(fun vb -> vb.Name)
        |> List.forall(fun (_,l) -> l = 1)
        |> equal true
  • Press F5 (or launch Run Mocha Tests) to debug this test

Web-App

  • Start with: ./fake.cmd run -t WebApp.Watch
  • Launch in Browser: localhost:8080

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:

  • Erased unions: TypeScript union types work differently from F# and its only purpose is to specify the types allowed for a function argument. In F# they are translated as erased unions: they're checked at compiled time but they'll be removed from the generated JS code.
type CanvasRenderingContext2D =
    abstract fillStyle: U3<string, CanvasGradient, CanvasPattern> with get, set

let ctx: CanvasRenderingContext2D = failwith "dummy"
ctx.fillStyle <- U3.Case1 "#FF0000"
  • Constructor functions: In JS any function can become a constructor just by calling it with the new keyword. In the parsed files, interfaces with this capability will have a Create method attached:
type CanvasRenderingContext2DType =
    abstract prototype: CanvasRenderingContext2D with get, set
    [<Emit("new $0($1...)")>] abstract Create: unit -> CanvasRenderingContext2D
  • Callable interfaces: In the same way, JS functions are just objects which means applying arguments directly to any object is legal in JS. To convey, the parser attaches an Invoke method to callable interfaces:
type Express =
    inherit Application
    abstract version: string with get, set
    abstract application: obj with get, set
    [<Emit("$0($1...)")>] abstract Invoke: unit -> Application

More Repositories

1

Fable

F# to JavaScript, TypeScript, Python, Rust and Dart Compiler
F#
2,916
star
2

fable-react

Fable bindings and helpers for React and React Native
F#
273
star
3

Fable.Python

Python bindings for Fable
F#
134
star
4

fable-elmish-electron-material-ui-demo

Complete boilerplate for Electron apps using Fable and Elmish with hot module reloading, time-travel debugging, etc.
F#
112
star
5

Fable.Lit

Write Fable Elmish apps with Lit
F#
94
star
6

fable2-samples

Fable 2 samples
F#
68
star
7

fable-browser

Fable bindings for Browser Web APIs
F#
66
star
8

repl

Fable online REPL
F#
61
star
9

samples-electron

Fable bindings and samples for Github Electron
F#
60
star
10

fable-arch

Framework for building applications based on the elm architecture.
F#
60
star
11

Fable.Store

Manage state logic in Svelte, React apps and more
F#
59
star
12

fable-react-native

Fable bindings and helpers for React Native projects
F#
49
star
13

fable3-samples

Nagareyama (Fable 3) samples
F#
47
star
14

Fable.Solid

Fable bindings for SolidJS
F#
45
star
15

samples-browser

Fable samples for browser apps
F#
36
star
16

fable-powerpack

Utilities for Fable apps
F#
33
star
17

fable-import

Fable bindings for JavaScript libraries
F#
33
star
18

fable-electron

Fable bindings for Electron apps
F#
32
star
19

Feliz.JSX

Feliz API for JSX compilation
F#
30
star
20

Fable.Jupyter

F# (Fable) support for Jupyter
Jupyter Notebook
30
star
21

Fable.JsonProvider

F# JSON Type Provider for Fable
F#
28
star
22

fable-templates

Templates for Fable projects maintained by fable-compiler org
F#
26
star
23

static-web-generator

Simple Fable Node.js app to generate static pages
F#
24
star
24

fable-compiler.github.io

Fable web site
JavaScript
23
star
25

fable-graphics

Fable bindings for graphic tools: D3, DC, Three, Pixi
F#
23
star
26

fable-node

Bindings for node.js native modules
F#
21
star
27

cfworker-hello-world

Cloudflare worker
F#
19
star
28

fable-promise

Fable bindings for JS promise
F#
19
star
29

Fable.Flutter

Fable bindings for Flutter API
F#
18
star
30

fable-fetch

Fable bindings for Browsers' Fetch API
F#
17
star
31

samples-pixi

Pixi samples for Fable
CSS
15
star
32

repl-legacy

F#
14
star
33

Fable.RegexProvider

Simple Safe Regex Provider compatible with Fable
F#
12
star
34

Fable.Expect

Utilities to test Fable apps
JavaScript
11
star
35

webpack-config-template

Template for webpack.config.js in Fable projects
JavaScript
10
star
36

ts2fable-exports

All branches are auto-generated by ts2fable
F#
9
star
37

fable-recharts

Fable bindings for Recharts
F#
8
star
38

fable-vscode-extension

Example of a VS Code Extension Written in F#
F#
8
star
39

Fable.I18Next

Fable bindings and helpers for [i18next](https://www.i18next.com/)
JavaScript
7
star
40

community

Finder for community resources
F#
7
star
41

fable-pixi

Bindings For Pixi.js
F#
7
star
42

Fable.ReactGoogleMaps

Fable bindings and helpers for react-google-map
F#
7
star
43

fableconf-workshops

Workshops for FableConf
F#
5
star
44

fableconf

Material for fableconf
F#
4
star
45

rollup-plugin-fable

Rollup plugin for Fable (F# to JS compiler)
JavaScript
4
star
46

repl2

REPL for Fable 2
F#
3
star
47

repl3

F#
3
star
48

Fable.Package.SDK

F#
3
star
49

fable-import-ws

Fable bindings for Websockets library ws
F#
2
star
50

fable-date

F#
2
star
51

fable-import-google-cloud-functions

F#
2
star
52

Fable.React.UseElmish

useElmish React hook
F#
1
star
53

fable-react-dom

F#
1
star