Warning: file_get_contents(/data/phpspider/zhask/data//catemap/1/typescript/9.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Typescript T扩展抽象类构造函数_Typescript - Fatal编程技术网

Typescript T扩展抽象类构造函数

Typescript T扩展抽象类构造函数,typescript,Typescript,我可以让这段代码工作吗(接口不是一个解决方案-这是一个简化的例子)?我不打算创建一个抽象类 TS2511:无法创建抽象类的实例 抽象类AbstractBase{ 私有只读[“AbstractBase”]; } 类A扩展了AbstractBase{} 类B扩展了AbstractBase{} 类C扩展了AbstractBase{} 函数create(c:T){返回新的c();} 根据定义,抽象类没有可调用的构造函数,因此typeof AbstractClass也不会有可调用的构造函数。您可以改用构造

我可以让这段代码工作吗(接口不是一个解决方案-这是一个简化的例子)?我不打算创建一个抽象类

TS2511:无法创建抽象类的实例

抽象类AbstractBase{
私有只读[“AbstractBase”];
}
类A扩展了AbstractBase{}
类B扩展了AbstractBase{}
类C扩展了AbstractBase{}
函数create(c:T){返回新的c();}

根据定义,抽象类没有可调用的构造函数,因此
typeof AbstractClass
也不会有可调用的构造函数。您可以改用构造函数

abstract class AbstractBase {
  private readonly ["AbstractBase"];
}


class A extends AbstractBase {}
class B extends AbstractBase {}
class C extends AbstractBase {}


function create<T extends new () => AbstractBase>(c: T) {return new c();}
抽象类AbstractBase{
私有只读[“AbstractBase”];
}
类A扩展了AbstractBase{}
类B扩展了AbstractBase{}
类C扩展了AbstractBase{}
函数create AbstractBase>(c:T){返回新的c();}

我使用了类似的解决方案,但这是一个黑客攻击。我想要一个漂亮的解决方案,而不需要编写额外的代码<代码>接口构造函数{new(…arg:any[]):T;prototype:T;}
类型hack=T扩展构造函数?T:Constructor&T
函数create(c:T){returnnewc();}
@tarnose不知道你为什么说这是一个黑客行为
typeof AbstractClass
是类本身,包含所有表示的内容,包括抽象性。构造函数签名表示返回特定类型的新实例的构造函数。对于非抽象类来说,它们有些重叠,而对于抽象类则没有。。这只是类型系统的工作方式。。这不是黑客
abstract class AbstractBase {
  private readonly ["AbstractBase"];
}


class A extends AbstractBase {}
class B extends AbstractBase {}
class C extends AbstractBase {}


function create<T extends new () => AbstractBase>(c: T) {return new c();}