如果您只想强制执行某些新变量,您可以指定构造函数的返回类型
interface Newable {
errorConstructor: new(...args: any) => Error; // <- put here whatever Base Class you want
}
相等的
declare class AnyError extends Error { // <- put here whatever Base Class you want
// constructor(...args: any) // you can reuse or override Base Class' contructor signature
}
interface Newable {
errorConstructor: typeof AnyError;
}
测试
class NotError {}
class MyError extends Error {}
const errorCreator1: Newable = {
errorConstructor: NotError, // Type 'typeof NotError' is missing the following properties from type 'typeof AnyError': captureStackTrace, stackTraceLimitts
};
const errorCreator2: Newable = {
errorConstructor: MyError, // OK
};