{"id":13723262,"url":"https://github.com/fable-compiler/ts2fable","last_synced_at":"2025-04-05T00:09:14.097Z","repository":{"id":37545953,"uuid":"86194697","full_name":"fable-compiler/ts2fable","owner":"fable-compiler","description":"Parser of Typescript declaration files","archived":false,"fork":false,"pushed_at":"2024-03-29T14:55:09.000Z","size":22037,"stargazers_count":221,"open_issues_count":72,"forks_count":35,"subscribers_count":16,"default_branch":"master","last_synced_at":"2024-04-14T12:57:45.897Z","etag":null,"topics":[],"latest_commit_sha":null,"homepage":"http://fable.io/ts2fable/","language":"F#","has_issues":true,"has_wiki":null,"has_pages":null,"mirror_url":null,"source_name":null,"license":"apache-2.0","status":null,"scm":"git","pull_requests_enabled":true,"icon_url":"https://github.com/fable-compiler.png","metadata":{"files":{"readme":"README.md","changelog":null,"contributing":null,"funding":null,"license":"LICENSE","code_of_conduct":null,"threat_model":null,"audit":null,"citation":null,"codeowners":null,"security":null,"support":null,"governance":null,"roadmap":null,"authors":null,"dei":null,"publiccode":null,"codemeta":null}},"created_at":"2017-03-25T23:21:25.000Z","updated_at":"2024-05-08T07:44:14.028Z","dependencies_parsed_at":"2024-03-23T15:30:14.538Z","dependency_job_id":"3fd298c7-fd96-4827-99fb-67ae45d84d1e","html_url":"https://github.com/fable-compiler/ts2fable","commit_stats":{"total_commits":387,"total_committers":24,"mean_commits":16.125,"dds":0.6253229974160206,"last_synced_commit":"01160b97cf62ef59ec21d7b6e0b2e0223cfd3b69"},"previous_names":[],"tags_count":11,"template":false,"template_full_name":null,"repository_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/fable-compiler%2Fts2fable","tags_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/fable-compiler%2Fts2fable/tags","releases_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/fable-compiler%2Fts2fable/releases","manifests_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/fable-compiler%2Fts2fable/manifests","owner_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/owners/fable-compiler","download_url":"https://codeload.github.com/fable-compiler/ts2fable/tar.gz/refs/heads/master","host":{"name":"GitHub","url":"https://github.com","kind":"github","repositories_count":247266564,"owners_count":20910836,"icon_url":"https://github.com/github.png","version":null,"created_at":"2022-05-30T11:31:42.601Z","updated_at":"2022-07-04T15:15:14.044Z","host_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub","repositories_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories","repository_names_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repository_names","owners_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/owners"}},"keywords":[],"created_at":"2024-08-03T01:01:38.033Z","updated_at":"2025-04-05T00:09:14.072Z","avatar_url":"https://github.com/fable-compiler.png","language":"F#","funding_links":[],"categories":["F#"],"sub_categories":[],"readme":"# **ts2fable**  [![npm version](https://badge.fury.io/js/ts2fable.svg)](https://www.npmjs.com/package/ts2fable)\n\n[Fable](https://github.com/fable-compiler/Fable) parser for [TypeScript declaration files](https://www.typescriptlang.org/docs/handbook/writing-declaration-files.html).\n## Usage\n\nInstall it with yarn or npm. With yarn it is:\n```\nyarn global add ts2fable\n```\n\nWith npm it is:\n```\nnpm install -g ts2fable\n```\nRun 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`.\n\n```\nyarn add @types/yargs --dev\nts2fable node_modules/@types/yargs/index.d.ts src/Yargs.fs\n```\n\nYou can also use `--export`(or `-e`) option to collect from multiple `tsfiles` \n\nIn 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](https://github.com/fable-compiler/ts2fable-exports/blob/master/OfficeReact.fs)\n```\nts2fable node_modules/office-ui-fabric-react/lib/index.d.ts test-compile/OfficeReact.fs -e uifabric office-ui-fabric-react\n```\n\nYou can find more information about how to interact with JavaScript\nfrom F# [here](https://fable.io/docs/communicate/js-from-fable.html).\nPlease note the parser is not perfect and some tweaking by hand may be needed. Please submit bugs as [issues on GitHub](https://github.com/fable-compiler/ts2fable/issues).\n\n### Online Version\nYou can also try an in-browser version [here](http://fable.io/ts2fable/)\n\nThe online version will be updated automatically when commits is merged\n\n\n## Contributing\nSuccesfull [builds](https://ci.appveyor.com/project/fable-compiler/ts2fable/history) on the master branch are uploaded and tagged as `next`. You can help us test these builds by installing them with:\n```\nyarn global add ts2fable@next\n```\n\nor build directly from source:\n- `git clone https://github.com/fable-compiler/ts2fable`\n- `cd ts2fable`\n- Compile:\n  - Windows: `./fake.cmd build`\n  - Linux: `./fake.sh build`\n- Run: \n  - `node -require esm ./build/cli/ts2fable.js Path/to/Declaration.d.ts Path/to/Output.fs`\n  - or: `npm ts2fable Path/to/Declaration.d.ts Path/to/Output.fs`\n\n`-require esm` is needed, because fable outputs code with ES modules.  \nIf you want to run ts2fable directly without esm (ECMAScript module loader): \n* build:  `./fake.cmd build -t Cli.BuildRelease`\n* call: `node ./dist/ts2fable.js ...`\n\n\n### Test Suites\n* `./test`: Mocha tests\n    * Run: `./fake.cmd build -t RunTest`\n    * Watch: `./fake.cmd build -t WatchAndRunTest`\n    * `functionTests.fs`: Test ts2fable functions\n    * `fsFileTests.fs`: Test file translation using small snippets and compare output against expected results or content\n* `./test-compile`: Translate actual TypeScript declaration files with ts2fable into F#. Then compile with F# compiler to ensure they are valid F#.\n    * Run: `./fake.cmd build -t BuildTestCompile`\n    * Setup for translation of .ts files: `./build.fsx` \u003e Target `RunCliOnTestCompile`\n    * Setup for .NET compilation: `./test-compile/test-compile.fsproj`\n\nRun both test suites: `./fake.cmd build -t CliTest`\n\n**Debug Test**:\n* In VS Code: `Ctrl+Shift+P` \u003e Run Task \u003e WatchTest\n* Add your test to `./test/fsFileTests.fs` and prefix with mocha `only` (See below sample)\n\nSample Test:\n```fsharp\nonly \"duplicated variable exports\" \u003c| fun _ -\u003e\n    let tsPaths = [\"node_modules/reactxp/dist/web/ReactXP.d.ts\"]\n    let fsPath = \"test-compile/ReactXP.fs\"\n    testFsFiles tsPaths fsPath  \u003c| fun fsFiles -\u003e\n        fsFiles\n        |\u003e getTopVariables\n        |\u003e List.countBy(fun vb -\u003e vb.Name)\n        |\u003e List.forall(fun (_,l) -\u003e l = 1)\n        |\u003e equal true\n```\n\n* Press F5 (or launch `Run Mocha Tests`) to debug this test\n\n### Web-App\n* Start with: ` ./fake.cmd run -t WebApp.Watch`\n* Launch in Browser: `localhost:8080`\n\n\n## Conventions\n\nSome JavaScript/TypeScript features have no direct translation to F#. Here is\na list of common workarounds adopted by the parser to solve these problems:\n\n* **Erased unions**: TypeScript union types work differently from F# and its only\npurpose is to specify the types allowed for a function argument. In F# they are\ntranslated as _erased unions_: they're checked at compiled time but they'll be\nremoved from the generated JS code.\n\n```fsharp\ntype CanvasRenderingContext2D =\n    abstract fillStyle: U3\u003cstring, CanvasGradient, CanvasPattern\u003e with get, set\n\nlet ctx: CanvasRenderingContext2D = failwith \"dummy\"\nctx.fillStyle \u003c- U3.Case1 \"#FF0000\"\n```\n\n* **Constructor functions**: In JS any function can become a constructor just by\ncalling it with the `new` keyword. In the parsed files, interfaces with this\ncapability will have a `Create` method attached:\n\n```fsharp\ntype CanvasRenderingContext2DType =\n    abstract prototype: CanvasRenderingContext2D with get, set\n    [\u003cEmit(\"new $0($1...)\")\u003e] abstract Create: unit -\u003e CanvasRenderingContext2D\n```\n\n* **Callable interfaces**: In the same way, JS functions are just objects which\nmeans applying arguments directly to any object is legal in JS. To convey, the\nparser attaches an `Invoke` method to callable interfaces:\n\n```fsharp\ntype Express =\n    inherit Application\n    abstract version: string with get, set\n    abstract application: obj with get, set\n    [\u003cEmit(\"$0($1...)\")\u003e] abstract Invoke: unit -\u003e Application\n```\n","project_url":"https://awesome.ecosyste.ms/api/v1/projects/github.com%2Ffable-compiler%2Fts2fable","html_url":"https://awesome.ecosyste.ms/projects/github.com%2Ffable-compiler%2Fts2fable","lists_url":"https://awesome.ecosyste.ms/api/v1/projects/github.com%2Ffable-compiler%2Fts2fable/lists"}