TypeScript 中的抽象构造函数类型
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/36886082/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me):
StackOverFlow
Abstract constructor type in TypeScript
提问by John Weisz
The type signature for a non-abstract class (non-abstract constructor function) in TypeScript is the following:
TypeScript 中非抽象类(非抽象构造函数)的类型签名如下:
declare type ConstructorFunction = new (...args: any[]) => any;
This is also called a newabletype. However, I need a type signature for an abstractclass (abstract constructor function). I understand it can be defined as having the type Function
, but that is waytoo broad. Isn't there a more precise alternative?
这也称为可新类型。但是,我需要一个抽象类(抽象构造函数)的类型签名。我的理解是可以被定义为具有类型Function
,但就是这样过于宽泛。没有更精确的选择吗?
Edit:
编辑:
To clarify what I mean, the following little snippet demonstrates the difference between an abstract constructor and a non-abstract constructor:
为了澄清我的意思,以下小片段演示了抽象构造函数和非抽象构造函数之间的区别:
declare type ConstructorFunction = new (...args: any[]) => any;
abstract class Utilities {
...
}
var UtilityClass: ConstructorFunction = Utilities; // Error.
Type 'typeof Utilities' is not assignable to type 'new (...args: any[]) => any'.
Cannot assign an abstract constructor type to a non-abstract constructor type.
类型“typeof Utilities”不可分配给类型“new (...args: any[]) => any”。
不能将抽象构造函数类型分配给非抽象构造函数类型。
回答by Tehau Cave
Was just struggling with a similar problem myself, and this seems to work for me:
我自己只是在为类似的问题而苦苦挣扎,这似乎对我有用:
type Constructor<T> = Function & { prototype: T }
回答by gaperton
Having the same problem. I guess, an essence of abstract class constructor signature is an absenseof new ( ... ) : X
thingy in its declaration. That's why it can be declared explicitly.
有同样的问题。我想,抽象类的构造函数签名的实质是由于缺少的new ( ... ) : X
啄在其声明。这就是为什么可以显式声明它的原因。
However. You can do this, and it will compile.
然而。你可以这样做,它会编译。
var UtilityClass: typeof Utilities = Utilities;
typeof Something
is a nice way to reference constructor types, however, it cannot be extended.
typeof Something
是一种引用构造函数类型的好方法,但是它不能被扩展。
And in any case you can do thing like this:
在任何情况下,您都可以这样做:
var UtilityClass: ConstructorFunction = <any> Utilities;
回答by Nitzan Tomer
The whole point with abstract classes (in OO in general) is that you can not instantiate them, you need a concrete non-abstract implementation.
抽象类(通常在 OO 中)的全部意义在于您无法实例化它们,您需要一个具体的非抽象实现。
I assume that you want to have different implementations to that abstract class and want to be able to receive one of those implementations (as a parameter or something of the likes).
If that's the case, then maybe this might solve your problem:
我假设您希望对该抽象类有不同的实现,并希望能够接收其中一个实现(作为参数或类似的东西)。
如果是这种情况,那么也许这可以解决您的问题:
declare type ConstructorFunction<T extends Utilities> = new (...args: any[]) => T;
abstract class Utilities { }
class MyUtilities extends Utilities { }
var UtilityClass: ConstructorFunction<MyUtilities> = MyUtilities;