Typescript 忽略或重写构造函数定义的参数

Typescript 忽略或重写构造函数定义的参数,typescript,dom,Typescript,Dom,我试图在单元测试中使用newtouch({identifier:Date.now(),target:elem,clientX:x}),但TypeScript抱怨出现错误2554:预期为0个参数,但得到1个 TS的最新版本具有触摸的正确定义: declare var Touch: { prototype: Touch; new(touchInitDict: TouchInit): Touch; }; 但我们的项目仍在使用版本2.9.2,该版本的定义不正确: declare var

我试图在单元测试中使用
newtouch({identifier:Date.now(),target:elem,clientX:x})
,但TypeScript抱怨出现
错误2554:预期为0个参数,但得到1个

TS的最新版本具有触摸的正确定义:

declare var Touch: {
    prototype: Touch;
    new(touchInitDict: TouchInit): Touch;
};
但我们的项目仍在使用版本2.9.2,该版本的定义不正确:

declare var Touch: {
    prototype: Touch;
    new(): Touch;
};

如何解决此问题?

您可以将
构造函数键入任意类型以解决此问题:

new (Touch as any)({ identifier: Date.now(), target: elem, clientX: x })
或者给它取个别名

const Touch2:any = Touch;
new Touch2({ identifier: Date.now(), target: elem, clientX: x })
或者,您可以向该别名添加正确的键入(首选)


感谢您的快速响应和各种选项。我曾尝试过
newtouch
,但没有想到
new(Touch是任何一款)
。其他选择也不错。如果您使用括号,触摸也同样有效。@MichaelBest非常欢迎:)。它确实可以工作,但是推荐的和更新的语法是“as”语法。引入它是因为“”语法与.tsx冲突。
interface Touch3Interface {
    prototype: Touch;
    new(touchInitDict: TouchInit): Touch;
}
const Touch3:Touch3Interface = Touch as any;
new Touch3({ identifier: Date.now(), target: elem, clientX: x })