我在两个单独的文件中有两个对象:
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.
*/
答案 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
是有利的。