haxiomic / dts2hx

Converts TypeScript definition files (d.ts) to haxe externs (.hx) via the TypeScript compiler API
MIT License
136 stars 9 forks source link

typedef with @:native metadata #48

Open clarkjones opened 4 years ago

clarkjones commented 4 years ago

when running dts2hx on puppeteer it generates a Request typedef that contains the following.

@:native('continue')
function continue_(?overrides:Overrides):js.lib.Promise<Void>;

From what I can tell this only works for classes & interfaces

haxiomic commented 4 years ago

Hey @clarkjones, you’re right and unfortunately haxe does not have a way to solve this yet! There is a proposal to support @:native on typedef fields so this may work in the near future

More discussion on the haxe repo: https://github.com/HaxeFoundation/haxe/issues/5105

clarkjones commented 4 years ago

thanks for the info. Creating an extern interface is not an option in dts2hx?

haxiomic commented 4 years ago

In this case an interface might do but not in the general case because we need the structure unification rules to apply, so a typedef gives you the most typescript-like behavior here

haxiomic commented 4 years ago

Just a note for future self, I think we can solve this by wrapping the typedefs with abstracts – not perfect but it will help as an interim before future compile changes fully resolve the issue

typedef StructureTypedef = {
    @:native('continue')
    function continue_(?overrides:Overrides):js.lib.Promise<Void>;
}

abstract Structure(StructureTypedef) {
    @:from static function fromTypedef(obj: StructureTypedef) {
        // apply @:native fields
    }
}