为什么 jquery.d.ts TypeScript 定义文件会抛出编译错误?
我在 VSTS 2012 中有一个基本的 MVC4 网络项目,用来尝试玩 TypeScript。
我安装了这个版本的 TypeScript:TypeScriptSetup.0.8.1.msi
我有来自以下地址的 jquery.d.ts 文件:http://typescript.codeplex.com/SourceControl/changeset/view/fe3bc0bfce1f#typings%2fjquery.d.ts
我在所有“索引签名”定义上都遇到了编译错误。第一个编译错误出现在 JQueryAjaxSettings 上:
headers?: { [key: any]: any; };
错误如下:
Error 9 Index signatures may only take 'string' or 'number' as their parameter
C:\Development\TypeScript\ScriptGen\ScriptGen.Web\Scripts\jquery.d.ts 37 18 app.ts
编译器不喜欢将 key 定义为 any 类型。
我应该将键类型改为字符串,还是我漏掉了什么?
答案#1
你正在使用旧版本的 jquery.d.ts。编译器自那时起已经更改,不允许 any 索引器。任何较新的 jquery.d.ts 版本(例如这个)都可以正常工作,或者如果你愿意,你也可以将那个 any 改为 string(不过在此期间该文件中还有多个其他修复,你可能也希望应用)。