2014-01-22 50 views
1

的X標籤規格有純JS以下簽名 -你如何定義打字稿接口簽名的X標籤組件

xtag.register('x-accordion', { 
    // extend existing elements 
    extends: 'div', 
    mixins: ['superdefaults', 'otherdefaults'], 
    lifecycle:{ 
     created: function(){ 
      // fired once at the time a component 
      // is initially created or parsed 
     }, 
     inserted: function(){ 
      // fired each time a component 
      // is inserted into the DOM 
     }, 
     removed: function(){ 
      // fired each time an element 
      // is removed from DOM 
     }, 
     attributeChanged: function(){ 
      // fired when attributes are set 
     } 
    }, 
    events: { 
     'click:delegate(x-toggler)': function(){ 
      // activate a clicked toggler 
     } 
    }, 
    accessors: { 
     'togglers': { 
      get: function(){ 
       // return all toggler children 
      }, 
      set: function(value){ 
       // set the toggler children 
      } 
     } 
    }, 
    methods: { 
     nextToggler: function(){ 
      // activate the next toggler 
     }, 
     previousToggler: function(){ 
      // activate the previous toggler 
     } 
    } 
}); 

其中明顯的方法,訪問者和事件可以有多個條目。

我想定義傳遞給寄存器函數的對象字面量的Typescript等價物。任何人有任何想法如何實現這一目標?

+1

你可以添加你已經嘗試過嗎?你需要哪方面的幫助? – WiredPrairie

回答

3

方法,訪問器和事件的祕密是使用字符串索引器。我只是要寫這部分,但最終無論如何都是最重要的,所以我把它完成了。

declare module xtag { 
    interface RegisterParams { 
     extends?: string; 
     prototype?: any; 
     mixins?: string[]; 
     lifecycle?: { 
      created?:() => void; 
      inserted?:() => void; 
      removed?:() => void; 
      attributeChanged?:() => void; 
     } 
     events: { [name: string]:() => void; }; 
     accessors: {[name: string]: { 
      get?:() => any; 
      set?: (value: any) => any; 
      attribute?: any; 
     }}; 
     methods: { [name: string]:() => void; } 
    } 
    export function register(tagName: string, params: RegisterParams): void; 
} 

索引器here有個不錯的解釋。在accessors上無法表達get和set與屬性互斥。我們可以做的最好的是讓他們可選。

+0

真棒,非常感謝Jeffrey! – Dunco