如何使用Typescript在Meteor中扩展/实现Mongo.Collection?

时间:2016-07-11 07:13:15

标签: meteor typescript

meteor/mongo.Mongo.Collection似乎是一个接口(根据these type declarations),网络上的大多数sample code都显示了如何扩展该接口,然后为方法中的方法编写代码单独的块:

  export interface List {
    _id?: string;
    name?: string;
    incompleteCount?: number;
  }

  export interface CollectionLists extends Mongo.Collection<List> {
    defaultName(): string;
  }

  export var Lists = <CollectionLists>(new Mongo.Collection<List>('lists'));

  // Calculate a default name for a list in the form of 'List A'
  Lists.defaultName = function(): string {
    var nextLetter = 'A', nextName = 'List ' + nextLetter;
    while (Lists.findOne({name: nextName})) {
      // not going to be too smart here, can go past Z
      nextLetter = String.fromCharCode(nextLetter.charCodeAt(0) + 1);
      nextName = 'List ' + nextLetter;
    }

    return nextName;
  };

有没有办法用类重写这个例子而不是这个接口,为所有添加的方法使用单独的代码块?

0 个答案:

没有答案