Why does the compiler keep interfaces in generated JS

If you look at Try Haxe ! and open the JS source tab you’ll see that the interface defined in the following Haxe code…

interface IMyClass {
    var myVar:String;

class MyClass implements IMyClass {
    static function main(){}
	public var myVar:String;

… appears in the generated JS…

// Generated by Haxe 3.4.4
(function () { "use strict";
var IMyClass = function() { };
var MyClass = function() { };
MyClass.__interfaces__ = [IMyClass];
MyClass.main = function() {

What is the purpose of generating interface related code in JS? I understand that interfaces are use for type checking at build time but I would expect that once the JS has been generated then any interface related code could be stripped in order to minimise the JS payload.

1 Like

It is used for run-time type checking such as Std.is, but I guess we could skip generating this in case Std.is itself is not present in the output…

What @nadako just said (damn, you’re quick :D)

You can add @:remove to your interface to avoid this.

I have an advantage of having a slack notification bot :stuck_out_tongue:

Thanks for the quick responses. Makes sense. Although adding @:remove to an interface could apparently break any future run-time type checking in a hard to debug way. You’d have to remember to remove the @:remove once you use Std.is

I created an issue on the Haxe repo so I don’t forget to look into it :slight_smile:

1 Like

Haxe 4 will be a bit smarter with this. Thanks for bringing this topic up :slight_smile: