我只是将typescript从v2.3升级到v2.4,现在它在knockout.validation.d.ts行上给出了一个错误:
interface KnockoutSubscribableFunctions<T> {
isValid: KnockoutComputed<boolean>;
isValidating: KnockoutObservable<boolean>;
rules: KnockoutObservableArray<KnockoutValidationRule>;
isModified: KnockoutObservable<boolean>;
error: KnockoutComputed<string>;
setError(error: string): void;
clearError(): void;
}
Run Code Online (Sandbox Code Playgroud)
这里knockout.validation试图表明KnockoutSubscribableFunction现在有额外的成员.以下是knockout.d.ts中此接口的定义:
interface KnockoutSubscribableFunctions<T> {
[key: string]: KnockoutBindingHandler;
notifySubscribers(valueToWrite?: T, event?: string): void;
}
Run Code Online (Sandbox Code Playgroud)
编译器现在抱怨:
'KnockoutComputed'类型的属性'isValid'不能赋予字符串索引类型'KnockoutBindingHandler'.
我不明白为什么它没有将这些新值视为界面中的新属性?为什么要说他们必须映射到索引签名?该文件似乎表明,你可以在同一个界面中的指数的签名和其他特性.
我把接口的初始定义带到了游乐场,它甚至抱怨notifySubscribers不能分配给KnockoutBindingHandler.
使用新编译器如何编译此代码?
现在有蛮力的方法让这个编译.我正在将knockout.d.ts定义更改为:
interface KnockoutSubscribableFunctions<T> {
[key: string]: any;//KnockoutBindingHandler;
notifySubscribers(valueToWrite?: T, event?: string): void;
}
Run Code Online (Sandbox Code Playgroud)
Mar*_*rev 23
由于以下类型的不同,存在问题:
[key: string]: KnockoutBindingHandler;
Run Code Online (Sandbox Code Playgroud)
和其他参数:
isValid: KnockoutComputed<boolean>;
isValidating: KnockoutObservable<boolean>;
rules: KnockoutObservableArray<KnockoutValidationRule>;
isModified: KnockoutObservable<boolean>;
error: KnockoutComputed<string>;
setError(error: string): void;
clearError(): void;
Run Code Online (Sandbox Code Playgroud)
你得到的错误基本上说:KnockoutComputed类型不能分配给KnockoutBindingHandler类型.
可能这个编译时检查在TS 2.4中得到了改进,这就是为什么你以前没有遇到过这个问题的原因.
您的解决方案有效
[key: string]: any;//KnockoutBindingHandler;
Run Code Online (Sandbox Code Playgroud)
如果您可以更改此代码,您可以尝试另一个更"漂亮"的解决方案:
[key: string]: any | KnockoutBindingHandler;
Run Code Online (Sandbox Code Playgroud)
这可能会为您提供一些额外的自动填充帮助.
| 归档时间: |
|
| 查看次数: |
3747 次 |
| 最近记录: |