Dattiloscritto: Implementazione di interfaccia nel costruttore possibile?

voti
4

Ho la seguente interfaccia:

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

E poi voglio per la sua attuazione come classe e le proprietà di essere impostato nel costruttore:

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

Tuttavia, secondo il compilatore richiesta non attua l'interfaccia. Non capisco come si controllarlo, mentre si ha riempito tutto secondo l'interfaccia in fase di costruzione e, se scritto in JavaScript questo avrebbe funzionato bene, tipo controllando la stessa cosa in strumenti di chiusura anche funziona perfettamente. L'idea è che voglio implementare l'interfaccia come una classe in modo da poter disporre di metodi di utilità nel prototipo, ma ancora in grado di convertire facilmente stringa JSON.

Qualche idea?

Grazie

È pubblicato 08/10/2012 alle 12:02
fonte dall'utente
In altre lingue...                            


1 risposte

voti
7

Il servizio di lingua analizzerà staticamente tua dichiarazione di vostra interfaccia, e perché hai espresso che essa richiede che il vostro headermembro, che dovrebbe far parte della dichiarazione di classe:

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

Non ti preoccupare, l'uscita javascript è molto più snella:

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;
})();
Risposto il 08/10/2012 a 13:48
fonte dall'utente

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