打字稿缺少属性TS2012

时间:2013-09-13 09:00:51

标签: knockout.js requirejs typescript

我在两个单独的文件中有两个对象:

import FooResults = require('fooresults');
class FooViewModel {
    Results = ko.observable<FooResults[]>();
}
export = FooViewModel;

class FooResults {
    Id = ko.observable<number>();
    Text = ko.observable<string>();
}
export = FooResults;

但是FooViewModel抱怨私人访问,所以我切换到了界面。

import FooResults = require('fooresults');
class FooViewModel {
    Results: KnockoutObservable<IFooResults[]> = ko.observable<FooResults[]>();
}
export = FooViewModel;

class FooResults implements IFooResults {
    Id: KnockoutObservable<number> = ko.observable<number>();
    Text: KnockoutObservable<string> = ko.observable<string>();
}
export = FooResults;

使用:

declare interface IFooResults {
    Id: KnockoutObservable<number>;
    Text: KnockoutObservable<string>;
}

但是现在编译器在编译FooViewModel时抱怨,说它无法将FooResults转换为IFooResults(尽管FooResults正好编译为js)。

什么错了?感谢。

编辑:这是实际错误......

/*

Compile Error. 
See error list for details
 D:/MyWebProject/Scripts/fooviewmodel.ts(18,4): error TS2012: Cannot convert 'KnockoutObservable<FooResults[]>' to 'KnockoutObservable<IFooResults[]>':

    Call signatures of types 'KnockoutObservable<FooResults[]>' and 'KnockoutObservable<Symology.Insight.ViewModels.StreetWorks.Works.IFooResults[]>' are incompatible:

        Type 'FooResults' is missing property 'Id' from type 'IFooResults'.

        Call signature expects 0 or fewer parameters.


*/

2 个答案:

答案 0 :(得分:0)

我删除了这个类,只使用了接口作为类型。也切换到observableArray

class FooViewModel {
    Results = ko.observableArray<IFooResults>();
}
export = FooViewModel;

答案 1 :(得分:0)

我怀疑错误在其他地方。以下代码段编译完全正常:

/// <reference path="./knockout.d.ts" />

class FooResults {
    Id = ko.observable<number>();
    Text = ko.observable<string>();
}

class FooViewModel {
    Results = ko.observable<FooResults[]>();
}

其中knockout.d.ts来自https://github.com/borisyankov/DefinitelyTyped/blob/master/knockout/knockout.d.ts

PS:据说使用observableArray是有利的。