我有一个名为canJS的第三方库。有这个can.Map模块。该模块是用AMD编写的。您可以使用依赖项加载器(例如requirejs)进行加载。
使用can.Map,我们提供了两种创建实例的方式。
let foo = new can.Map([props])
let Foo = can.Map.extend([name,] [staticProperties,] instanceProperties); let foobar = new Foo();
我试图为此类编写一个简单的类型定义
declare module "can/map/" {
class canMap {
}
}
但现在我坚持了。
这种类型的定义看起来如何。我总是收到这样的错误:
不能对类型缺少调用或构造签名的表达式使用'new'
您可以使用适当的文档来编写声明文件。
我认为您的情况将是这样的:
declare module can {
class Map {
constructor(props: any);
static extend(name, staticProperties, instanceProperties): Map;
attr(): {[index: string]: any};
// Declarations for other properties and functions
}
}
用法(当然,应加载“ canjs”):
var a = new can.Map({});
var b = can.Map.extend({}, {}, {});
var c = a.attr();
最好的办法是上找不到明确键入声明definitelytyped.org。几乎所有流行的库定义都已经被编写。
您可以使用分型为“d.ts”文件管理。
可选参数(标记为“?”)和静态函数“ extend”返回构造函数(带有重载):
declare module can {
class Map {
constructor(props?: any);
static extend(instanceProperties): { new(): Map };
static extend(name, staticProperties, instanceProperties): { new(): Map };
attr(): {[index: string]: any};
// Declarations for other properties and functions
}
}
let creator1 = can.Map.extend({});
let creator2 = can.Map.extend({}, {}, {});
var c = new creator1();
当然,您应该指定函数参数的类型(现在,thay是“ any”类型的)。您可以在Typescript文档中找到更多详细信息。
本文收集自互联网,转载请注明来源。
如有侵权,请联系[email protected] 删除。
我来说两句