TypeScript中的抽象构造函数类型

TypeScript中的抽象构造函数类型

本文介绍了TypeScript中的抽象构造函数类型的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

TypeScript中非抽象类(非抽象构造函数)的类型签名如下:

The type signature for a non-abstract class (non-abstract constructor function) in TypeScript is the following:

declare type ConstructorFunction = new (...args: any[]) => any;

这也称为 newable 类型。但是,我需要一个 abstract 类的类型签名(抽象构造函数)。我知道可以将其定义为具有 Function 类型,但是 way 范围太广。

This is also called a newable type. However, I need a type signature for an abstract class (abstract constructor function). I understand it can be defined as having the type Function, but that is way too broad. Isn't there a more precise alternative?

编辑:

为阐明我的意思,以下小片段演示了抽象构造函数和非抽象构造函数之间的区别:

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.





推荐答案

我自己只是在努力解决类似的问题,

Was just struggling with a similar problem myself, and this seems to work for me:

type Constructor<T> = Function & { prototype: T }

这篇关于TypeScript中的抽象构造函数类型的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!

08-18 14:36