Maszynopis: Implementacja interfejsu w konstruktorze możliwe?

głosy
4

Mam następujący interfejs:

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

A potem chcę wdrożyć go jako klasy i właściwości są ustawione w konstruktorze:

  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);
    }
  }

Jednak zgodnie z wnioskiem kompilator nie realizuje interfejs. Nie rozumiem, jak to sprawdzić, podczas gdy to wszystko wypełnione zgodnie z interfejsem w fazie budowy, a jeśli napisane w JavaScript to będzie działać grzywny, typ sprawdzania samo w narzędzia zamykające również działa idealnie. Chodzi o to, że chcę, aby zaimplementować interfejs jako klasa, więc mogę mieć metod eksploatacji w prototypie, ale nadal będą mogli łatwo przekonwertować do formatu JSON ciąg.

Jakieś pomysły?

Dzięki

Utwórz 08/10/2012 o 12:02
źródło użytkownik
W innych językach...                            


1 odpowiedzi

głosy
7

Usługa język statycznie będzie analizować swoją deklarację interfejsu, a ponieważ pan wyraził, że wymaga się, że headerczłonek, który powinien stanowić część deklaracji klasy:

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";
    }
}

Nie martw się, wyjście JavaScript jest dużo szczuplejsze:

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;
})();
Odpowiedział 08/10/2012 o 13:48
źródło użytkownik

Cookies help us deliver our services. By using our services, you agree to our use of cookies. Learn more