dts2hx icon indicating copy to clipboard operation
dts2hx copied to clipboard

Module-level field support or improve class based workaround

Open haxiomic opened this issue 4 years ago • 0 comments

In TypeScript we have module-level fields like:

declare const cheerio: cheerio.CheerioAPI;
export = cheerio;

Ideally we can represent this in haxe as:

 @:jsRequire("cheerio") extern final cheerio: cheerio.CheerioAPI;

But @:jsRequire() doesn't yet work for module-level-fields

A work around is to convert this to a class that just contains a value:

@:jsRequire("cheerio") extern class Cheerio {
	static var value(get, never) : cheerio.CheerioAPI;
	static inline function get_value():cheerio.CheerioAPI return cast Cheerio;
}

This works but is clunky, it would be better to instead promote the fields of the variable into static fields of this class, for example, cheerio has a function, load():

@:jsRequire("cheerio") extern class Cheerio {
	static extern inline function load(arg) value.load(arg);

	static var value(get, never) : cheerio.CheerioAPI;
	static inline function get_value():cheerio.CheerioAPI return cast Cheerio;
}

However, to support overloads, we require the overload keyword which is only available in haxe 4.2+

It may make sense to spend effort getting module-level extern fields to work in haxe rather than improve this workaround

haxiomic avatar Apr 27 '21 13:04 haxiomic