1

x-tag 规范在纯 JS 中具有以下签名 -

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

显然方法、访问器和事件可以有多个条目。

我正在尝试定义传递给 register 函数的对象文字的 Typescript 等效项。任何人有任何想法如何实现这一目标?

4

1 回答 1

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

这里对索引器有一个不错的解释。没有办法表示 get 和 set与accessors属性是互斥的。我们能做的最好的事情就是让它们成为可选的。

于 2014-01-22T16:39:01.817 回答