TypeScript Declaration Generator for Haxe/JS
STATUS: WIP usable with limitations, feedback welcome!
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).
-lib hxtsdgen to compiler arguments and it'll do the rest.
- Generate concrete enums file (see below):
- 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.
- Anonymous Structure Typedefs
- Option to generate interfaces from Typedefs?
- Other uses of typedefs
- Properties (as
- Native properties (needs compiler support)
- Enums (needs compiler support)
- Abstract enums (see limitations)
- Abstract enums as concrete
- Option to generate "fat enums" from Abstract enums?
- More general abstracts (unlikely, excepted possibly for return values)
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.
hxtsdgen can generate TypeScript const enums,
which are a pure compiler construction, substitued at compile time with their value.
.ts code can be compiled by 2 compilers: TypeScript and Babel.
- Check Babel's
const enumsupport: https://github.com/babel/babel/issues/8741
- TypeScript supports
.d.tsfiles only in full compilations.
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
Abstracts and (regular) Enums
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.
Haxe JS doesn't generate native properties in any language - they are always transformed
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)