我有一个名为 canJS 的第三方库。 有这个can.Map模块。该模块是用 AMD 编写的。您可以使用依赖项加载器加载它,例如需要js。
通过 can.Map,我们提供了两种创建实例的方法。
let foo = new can.Map([props])
let Foo = can.Map.extend([name,] [staticProperties,] instanceProperties); 让 foobar = new Foo();
我尝试为此类编写一个简单的类型定义
declare module "can/map/" {
class canMap {
}
}
但现在我卡住了。
这种类型定义怎么样。 我总是收到这样的错误:
Cannot use 'new' with an expression whose type lacks a call or construct signature
最佳答案
您可以使用适当的documentation编写声明文件。
对于你的情况,我认为,它会是这样的:
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();
更新 1
最好的方法是在 definitelytyped.org 上找到明确键入的声明。几乎所有流行的库定义都已编写完毕。
您可以使用typings用于“d.ts”文件管理。
更新2
可选参数(“?”标记)和静态函数“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();
当然,您应该指定函数参数的类型(现在它们是“任何”类型)。 您可以在 Typescript documentation 中找到更多详细信息.
关于typescript - 第三方类的类型定义,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/36398457/