Skip to content
TypeScript declaration file generator for Haxe JavaScript output target
Haxe Shell
Branch: master
Clone or download
Fetching latest commit…
Cannot retrieve the latest commit at this time.
Permalink
Type Name Latest commit message Commit time
Failed to load latest commit information.
.vscode
src/hxtsdgen Friendly compiler option to set the file header (#24) Jul 10, 2019
test
.gitignore Friendly compiler option to set the file header (#24) Jul 10, 2019
.travis.yml Bump Haxe 3.x to 3.4.7 - 3.4.0 seem to arrange property accessors dif… May 24, 2019
CHANGES.md Release 0.2.0 Jun 25, 2019
CONTRIBUTING.md add a short notice about test cases Feb 21, 2017
README.md Friendly compiler option to set the file header (#24) Jul 10, 2019
complete.hxml Release 0.1.1 Jun 19, 2019
extraParams.hxml make it more like a proper haxelib Feb 21, 2017
haxelib.json Release 0.2.0 Jun 25, 2019
submit.sh haxelib missing `extraParams.hxml` Jun 24, 2019
test.hxml Friendly compiler option to set the file header (#24) Jul 10, 2019
test.sh Release 0.1.1 Jun 19, 2019

README.md

TypeScript Declaration Generator for Haxe/JS

Build Status

STATUS: WIP usable with limitations, feedback welcome!

This is a TypeScript declaration file generator for the Haxe JavaScript output target.

What it does is generate a .d.ts file next to the .js file generated by Haxe compiler, containing TypeScript declarations for classes and functions exposed from Haxe using the @:expose metadata, and all their related types (function arguments and return types).

Why?

To make using Haxe/JS modules from both JavaScript and TypeScript much easier, of course! Just compile your Haxe library to a JS module and use it in TypeScript in a perfectly typed way.

Usage

Just add -lib hxtsdgen to compiler arguments and it'll do the rest.

Options:

  • Generate concrete enums file (see below): -D hxtsdgen_enums_ts
  • Custom file header: --macro hxtsdgen.Generator.setHeader('/* tslint:disable */')

Supported Haxe features

Certain Haxe features are compiler abstractions and thus may not be possible to translate to TypeScript.

  • Classes
  • Interfaces
  • Anonymous Structure Typedefs
  • Option to generate interfaces from Typedefs?
  • Other uses of typedefs
  • Properties (as get_prop/set_prop)
  • Native properties (needs compiler support)
  • Enums (needs compiler support)
  • Abstract enums (see limitations)
  • Abstract enums as concrete .ts enums (-D hxtsdgen_enums_ts)
  • Option to generate "fat enums" from Abstract enums?
  • More general abstracts (unlikely, excepted possibly for return values)

Limitations

No automatic export

Currently all the types have to be explictly exposed; types used by functions won't be automatically exported so everything must be currently annotated for export.

Abstract enums

hxtsdgen can generate TypeScript const enums, which are a pure compiler construction, substitued at compile time with their value.

However .ts code can be compiled by 2 compilers: TypeScript and Babel.

For optimal compiler compatibility, add -D hxtsdgen_enums_ts to hint hxtsdgen to generate enums in a separate .ts file which will be compatible with --transpileOnly --preserveConstEnums.

Abstracts and (regular) Enums

Enums and Abstracts are a key feature of the Haxe language, but They don't translate well to JavaScript APIs as they are a compiler feature.

General Abstracts, offering compile-time types abstractions and operators overloading will probably never be supported.

Enums could be somehow supported, but currently the compiler doesn't allow exposing them.

Native properties

Haxe JS doesn't generate native properties in any language - they are always transformed into get_prop/set_prop functions. It is possible that in the future the Haxe compiler will allow exporting native properties.

How does it look?

Check out these 3 awesome panes (left-to-right):

  • Main.hx (Haxe source code)
  • main.d.ts (generated TypeScript declarations)
  • test.ts (TypeScript source code that uses the declarations)

You can’t perform that action at this time.