我一直试图为一个外部的JavaScript包编写类型,该包提供一个大对象,其属性(主要)是一堆不同的类构造函数.一些类似的事情
class ThingOne {
constructor(par1, par2) { // guts expecting par1 to be a ThingTwo and par2 a number... }
aMethod(par) { // wants a string... }
// etc etc
}
class ThingTwo {
constructor(par1, par2) { // wants a string and a boolean... }
// etc etc
}
var bigObject = { \\ this is what comes out of the package
ThingOne: ThingOne,
ThingTwo: ThingTwo
}
(在现有套餐中),其预期用途为
const myTwo = new bigObject.ThingTwo('foo', true);
const myOne = new bigObject.ThingOne(myTwo, 7);
// etc.
因此,在.d.ts文件中,我正在手工编写代码,并try 编写一个接口,用来输入bigObject
.我的第一次try 是
export interface BigObjectInterface {
ThingOne(par1: ThingTwo, par2: number): ThingOne;
ThingTwo(par1: string, par2: boolean): ThingTwo
}
export class ThingOne {
constructor(par1: ThingTwo, par2: number);
aMethod(par: string); // etc.
}
export class ThingTwo {
constructor(par1: string, par2: boolean);
// etc.
}
但正如我所期望的那样,这不起作用,因为它为用法键入
const myTwo = bigObject.ThingTwo('foo', true);
与预期的new
操作员不同.另一方面,接口中的new
不带方法名,所以我不知道如何使用它.我也不能将BigObjectInterface
中的ThingOne
属性仅键入为ThingOne
(例如ThingOne: ThingOne
),因为它不是ThingOne
对象,它只是ThingOne
的构造函数的别名(例如bigObject.ThingOne.aMethod('bar')
不合法).
那么,如何才能将BigObjectInterface
中的ThingOne
和ThingTwo
属性分类呢?