0

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

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说它不能转换FooResultsIFooResults(尽管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.


*/
4

2 回答 2

0

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

class FooViewModel {
    Results = ko.observableArray<IFooResults>();
}
export = FooViewModel;
于 2013-09-13T11:35:07.913 回答
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

于 2013-09-13T13:55:04.250 回答