Anonymous/inline interface implementation in TypeScript Anonymous/inline interface implementation in TypeScript javascript javascript

Anonymous/inline interface implementation in TypeScript


OK, I finally discovered the problem to question 2 - I was using the fat arrow => to declare the object's method here:

doThatThing(<Doable>{     private message: 'ahoy-hoy!',     do: () => { // using fat arrow: global scope replaces new object's scope        alert(this.message);    }});

...which "sucked" the global scope into the method. The problem is fixed using the longer syntax, like so:

doThatThing(<Doable>{    private message: 'ahoy-hoy!',    do: function() { // using "regular" anonymous function syntax, "this" meaning is preserved        alert(this.message);    }});

So in summary:

  1. unanswered;
  2. There was a typo in my code, and I should have been using "function()" instead of "=>"; and,
  3. Type-asserting the object with the interface removes the compiler error.