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
User | Most Recent Commit | # Commits |
---|
Other Committers
User | Most Recent Commit | # Commits |
---|
Fable parser for TypeScript declaration files.
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.
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
Windows:
git clone https://github.com/fable-compiler/ts2fable
fake.cmd run build.fsx
Unix:
git clone https://github.com/fable-compiler/ts2fable
./fake.sh run build.fsx
Common to all OS
Ctrl+Shift+P
> Run Task > WatchTesttest/fsFileTests.fs
and prefix it with mocha only
(See below sample)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
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"
new
keyword. In the parsed files, interfaces with this
capability will have a Create
method attached: CanvasRenderingContext2DType =
abstract prototype: CanvasRenderingContext2D with get, set
[<Emit("new $0($1...)")>] abstract Create: unit -> CanvasRenderingContext2D
Invoke
method to callable interfaces: Express =
inherit Application
abstract version: string with get, set
abstract application: obj with get, set
[<Emit("$0($1...)")>] abstract Invoke: unit -> Application