TypeScript重写成员不是原始成员的子类型

时间:2012-10-22 06:48:41

标签: backbone.js typescript

我正在使用TypeScript和BackboneJS开发测试应用程序。它主要是一个员工目录。在尝试定义集合时,我收到以下错误。

Type of overridden member 'url' is not subtype of original member defined by type 'Collection'

这是我的模型和集合定义。

class Employee extends Backbone.Model {
    reports: EmployeeCollection;

    constructor (options? ) {
        super(options);
        this.reports = new EmployeeCollection();
        this.reports.url = '../api/employees/' + this.id + '/reports';
    }
}

class EmployeeCollection extends Backbone.Collection {
    url: string = "../api/employees";
    model = Employee;
    findByName(key) {}
}

我也使用一些定义来连接Backbone和TypeScript,这里是我正在使用的文件。

export class Model {

    static extend(properties: any, classProperties?: any): any; // do not use, prefer TypeScript's extend functionality

    constructor (attributes?: any, options?: any);

    get(attributeName: string): any;
    set(attributeName: string, value: any): void;
    set(obj: any): void;

    escape(attribute);
    has(attribute);
    unset(attribute, options? );
    clear(options? );

    id: any;
    idAttribute: any;
    cid;
    attributes;
    changed;

    bind(ev: string, f: Function, ctx?: any): void;  /// ????

    // defaults; defaults();
    toJSON(): string;
    fetch(options? );
    save(attributes? , options? ): void;
    destroy(options? ): void;
    validate(attributes);
    isValid();
    url();
    urlRoot();
    parse(response);
    clone();
    isNew();
    change();
    hasChanged(attribute? );
    changedAttributes(attributes? );
    previous(attribute);
    previousAttributes();
}

export class Collection {

    static extend(properties: any, classProperties?: any): any; // do not use, prefer TypeScript's extend functionality

    model;

    constructor (models? , options? );

    models;
    toJSON(): any;

    ///// start UNDERSCORE 28:
    bind(ev: string, f: Function, ctx?: any): void;
    collection: Model;
    create(attrs, opts? ): Collection;
    each(f: (elem: any) => void ): void;
    last(): any;
    last(n: number): any[];
    filter(f: (elem: any) => any): Collection;
    without(...values: any[]): Collection;

    // Underscore bindings

    each(object: any, iterator: (value, key, list? ) => void , context?: any): any[];
    forEach(object: any, iterator: (value, key, list? ) => void , context?: any): any[];
    map(object: any, iterator: (value, key, list? ) => void , context?: any): any[];
    reduce(list: any[], iterator: any, memo: (memo: any, element: any, index: number, list: any[]) => any, context?: any): any[];
    reduceRight(list: any[], iterator: (memo: any, element: any, index: number, list: any[]) => any, memo: any, context?: any): any[];
    find(list: any[], iterator: any, context?: any): any; // ???
    detect(list: any[], iterator: any, context?: any): any; // ???
    filter(list: any[], iterator: any, context?: any): any[];
    select(list: any[], iterator: any, context?: any): any[];
    reject(list: any[], iterator: any, context?: any): any[];
    every(list: any[], iterator: any, context?: any): bool;
    all(list: any[], iterator: any, context?: any): bool;
    any(list: any[], iterator?: any, context?: any): bool;
    some(list: any[], iterator?: any, context?: any): bool;
    contains(list: any, value: any): bool;
    contains(list: any[], value: any): bool;
    include(list: any, value: any): bool;
    include(list: any[], value: any): bool;
    invoke(list: any[], methodName: string, arguments: any[]): any;
    invoke(object: any, methodName: string, ...arguments: any[]): any;
    max(list: any[], iterator?: any, context?: any): any;
    min(list: any[], iterator?: any, context?: any): any;
    sortBy(list: any[], iterator?: any, context?: any): any;
    sortedIndex(list: any[], valueL: any, iterator?: any): number;
    toArray(list: any): any[];
    size(list: any): number;
    first(array: any[], n?: number): any;
    initial(array: any[], n?: number): any[];
    rest(array: any[], n?: number): any[];
    last(array: any[], n?: number): any;
    without(array: any[], ...values: any[]): any[];
    indexOf(array: any[], value: any, isSorted?: bool): number;
    shuffle(list: any[]): any[];
    lastIndexOf(array: any[], value: any, fromIndex?: number): number;
    isEmpty(object: any): bool;
    groupBy(list: any[], iterator: any): any;

    add(models, options? );
    remove(models, options? );
    get(id);
    getByCid(cid);
    at(index: number);
    push(model, options? );
    pop(options? );
    unshift(model, options? );
    shift(options? );
    length: number;
    //comparator;
    sort(options? );
    pluck(attribute);
    where(attributes);
    url();
    parse(response);
    fetch(options?: any): void;
    reset(models, options? );
    create(attributes, options? );
}

export class Router {

    static extend(properties: any, classProperties?: any): any; // do not use, prefer TypeScript's extend functionality

    routes;
    constructor (options? );
    route(route, name, callback? );
    navigate(fragment, options? );
} 

1 个答案:

答案 0 :(得分:3)

'url'字段被声明为方法,但它可以是方法或属性。

更改
url(); 

url;

或者从这里下载定义(我刚刚更新过): https://github.com/borisyankov/DefinitelyTyped/blob/master/Definitions/backbone-0.9.d.ts