4

I have the following interface:

interface SMJSPacket {
  header: {
    tag: string;
    method: string;
    type: string;
  };
  response?: {
    status: string;
    content: string;
  };
  event?: {
    key?: string;
    action?: string;
  };
  request?: {
    run?: string;
  };
}

And then I want to implement it as a class and the properties being set in the constructor:

  class Request implements SMJSPacket {
    constructor(data: any, method: string) {
      this.header = {
        type: 'request',
        method: method || 'calld',
        tag: Request.getTag()
      }
      this.request = data;
    }
    static getTag(): string {
      return '_' + goog.now() + '_' + utils.getRandomBetween(1, 1000);
    }
  }

However according to the compiler Request is not implementing the interface. I don't understand how does it check it, whilst it has everything filled according to the interface at the construction phase and if written in JavaScript this would work fine, type checking the same thing in Closure tools also works perfectly. The idea is that I want to implement the interface as a class so I can have utility methods in the prototype but still be able to easily convert to JSON string.

Any ideas?

Thanks

1 Answer 1

7

The language service will statically analyse your declaration of your interface, and because you've expressed that it requires that your header member, that should form part of the class declaration:

class Request implements SMJSPacket {
    header: { tag: string; method: string; type: string; };

    constructor(data: any, method: string) {
        this.header = {
            type: "request",
            method: (method || "calld"),
            tag: Request.getTag()
        };
    }

    static getTag(): string {
        return "tag stuff";
    }
}

Don't worry, the output javascript is a lot leaner:

var Request = (function () {
    function Request(data, method) {
        this.header = {
            type: "request",
            method: (method || "calld"),
            tag: Request.getTag()
        };
    }
    Request.getTag = function getTag() {
        return "tag stuff";
    }
    return Request;
})();
Sign up to request clarification or add additional context in comments.

2 Comments

so what's the use of implements Interface?
@TSR: An interface is a contract that the class derived from the interface has to fullfill. The interface says: you class need method X and property Y to implement me, the compiler will fail if it doesn't. e.g.: Interface Vehicle, Classes Car and Bike. Both have at least to implement a method "drive" and a property "tires". Matthew Abbott: Aren't members in typescript the entirety of class components (properties, methods, constructor)? IMO "header property" would be the correct term. Also: One "that" to much, isn't it? Just saw the date of the answer. I'm a gravedigger by heart.

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.