TypeScript中的抽象构造函数类型 [英] Abstract constructor type in TypeScript

查看:141
本文介绍了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.




类型'typeof Utilities'不能分配给'new(。 ..args:any [])=> any'。

Type 'typeof Utilities' is not assignable to type 'new (...args: any[]) => any'.


不能将抽象构造函数类型分配给非抽象构造函数类型。 / p>

Cannot assign an abstract constructor type to a non-abstract constructor type.



推荐答案

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

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

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

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

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆