【发布时间】:2014-05-13 15:58:11
【问题描述】:
我正在尝试为 ASP.NET Ajax 库创建一个 TypeScript 1.0.2(VS 2013 Update 2 RTM)定义文件,但我对如何定义 MS Ajax 添加到的其他方法感到困惑基本 JS 类型,例如 Array。我创建了一个 AspNetAjax.d.ts 和一个 AspNetAjax-tests.ts 文件。当我尝试在测试文件中使用“add”方法时,我得到下面列出的编译器错误。
AspNetAjax.d.ts
interface Array<T> {
add(array: T[], item: T): void;
}
AspNetAjax-tests.ts
///<reference path="AspNetAjax.d.ts" />
var a: string[] = ['a', 'b', 'c', 'd'];
Array.add(a, 'e');
console.log(a.toString());
错误 1 '{ isArray(arg: any): boolean; 类型的值不存在属性 'add'原型:任何[]; (arrayLength?: number): any[]; (arrayLength: number): T[]; (...项目: T[]): T[];新的(数组长度?:数字):任何[];新的(数组长度:数字):T[];新的(...项目:T[]):T[]; }'。 c:\path\AspNetAjax-tests.ts 4 7 TypeScriptHTMLApp1
来自同一 d.ts 文件的其他定义在测试文件中有效,因此我知道该引用在物理上有效。 TypeScript 也不会抱怨我声明 d.ts 文件的方式(那里没有红色曲线)。
我知道这些其他问题,并且我认为我正在按照他们的建议进行操作,但似乎它们是从 2012 年末/2013 年初开始的,所以从那时起执行此操作的方式可能发生了变化?
Adding a property to Array in Typescript
How can I add a static method to an existing type?
我需要对 d.ts 进行编码,以便 .ts 文件中的代码能够正常工作。有什么想法吗?
【问题讨论】:
-
非常简单的解决方案 - 我已经在这里发布了:stackoverflow.com/questions/14867649/…
标签: webforms typescript