打字稿编译器bug?knockout.validation.d.ts不再编译

Gre*_*res 19 typescript

我只是将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)

这可能会为您提供一些额外的自动填充帮助.

  • PS.如果您在Visual Studio中构建,可以通过两种方式配置skipLibCheck:您可以添加tsconfig.json并指定"skipLibCheck":在compilerOptions下为true或在.csproj文件中添加msbuild参数:<TypeScriptSkipLibCheck> True </TypeScriptSkipLibCheck> (5认同)
  • 自从从TS 2.1升级到TS 2.5以来,我遇到了与knockout.d.ts相同的问题.就像Mark所说,TypeScript 2.4引入了额外的类型检查,它使用当前的knockout.d.ts(在我的例子中为knockout.TypeScript.DefinitelyTyped v1.1.6)暴露了一些潜在的"类型问题".这个问题的一个解决方案似乎是将skipLibCheck设置为true,例如在tsconfig.json中 (2认同)