类型 X 不是 'constructor function type' (TS2507)

Type X is not a 'constructor function type' (TS2507)

在设计定义文件时,我遇到了这个错误 (TS2507)。

如何将类型指定为 'constructor function type'?

嗯,如果您查看错误的原始定义 (https://github.com/Microsoft/TypeScript/blob/master/tests/baselines/reference/classExtendingPrimitive.errors.txt),您会发现 'undefined' 已硬编码在此错误消息中。 undefined 从来都不是有效的构造函数类型(即使在 js 中也是如此)。所以,我想你不需要做任何事情,只要确保你传递了一个有效的构造函数而不是未定义的响应。无效的。可以在此处找到此类问题的示例:https://www.codecademy.com/forum_questions/52f67024282ae3a0890009b0

如果你正在定义一个接口,你可以像这样声明它是一个构造函数:

interface SomeInterface {
  new(someParam: any): SomeInterface
}

这在您为已经存在的 JS 库定义类型时很有用。有关详细信息,请参阅 this SO answer