- Java 双重比较
- java - 比较器与 Apache BeanComparator
- Objective-C 完成 block 导致额外的方法调用?
- database - RESTful URI 是否应该公开数据库主键?
我正在实现一个 commandBus,我想用命令处理程序注册命令类型,以将传入的命令映射到正确的命令处理程序。我传递了一个命令构造函数来映射到一个处理程序构造函数,就像这样 register(handler : typeof Handler, command : typeof Command)
。但我不断收到编译错误。
我终于查出原因了。在 typescript 中,您不能定义参数 arg typeof X
并传入构造函数 Y
,事件 if 'Y extends X
。 构造函数必须具有相同的签名。
检查该片段。在底部,即使 commandBus.register(Object)
也不会抛出编译错误。
class BaseClass{}
class DerivedClass extends BaseClass{
constructor(b : number) {
super();
}
}
class AnotherClass extends BaseClass{
constructor(){
super();
}
}
class CommandBus {
register(handler : typeof BaseClass) {}
}
var commandBus = new CommandBus();
commandBus.register(DerivedClass); // Argument of type 'typeof DerivedClass' is not assignable to parameter of type 'typeof BaseClass'.
commandBus.register(AnotherClass); // Compiles
commandBus.register(Function); // Compiles
commandBus.register(Object); // Compiles
我让它工作的唯一方法是添加一个重载构造函数签名
class DerivedClass extends BaseClass{
constructor(b? : any);
constructor(b : number) {
super();
}
}
但是是我还是这太丑了?
任何人都可以指出如何消除这些编译器错误,而无需到处添加无用的重载签名的肮脏技巧吗?
最佳答案
你在这里真正想说的是你想要一个构造函数用于一个BaseClass
,它接受任意数量的参数。你可以这样写:
class CommandBus {
register(handler: new(...args: any[]) => BaseClass) {}
}
请注意,错误是 100% 正确的。如果你写了
class CommandBus {
register(handler : typeof BaseClass) {
var x = new handler();
}
}
var c = new CommandBus();
c.register(DerivedClass);
您将向 DerivedClass
构造函数传递零参数。
这些行在这里
commandBus.register(Function); // Compiles
commandBus.register(Object); // Compiles
只编译因为 BaseClass
没有成员(记住,TypeScript 使用结构类型系统,所以空类型可以从任何类型分配!)。如果您将任何不在 Function
或 Object
中的属性或方法添加到 BaseClass
,这些行将成为错误。
关于inheritance - Typescript 不允许具有不同构造函数签名的类的 typeof 继承,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/33442687/
我正在为我的项目测试 FlowJS,我遇到了 Type Aliases 的问题. 我为 API 构建了一个 Web SDK:我有一个使用此方法的 Helper 类: class Helper {
我是 Dart 的新手,仍在学习它带来的所有细微差别。我当前的任务真正困扰的一件事是从父类继承(或代理)静态方法。 void main() { EnglishCasual.greet(); } c
我正在尝试巩固 Go 提供的继承概念(也许是“组合”而不是纯粹的继承)。但是,我无法理解为什么我不能将“父”类型用作 func 参数来生成作用于该参数的通用函数。 package main impor
我在准确表述它时遇到问题,所以我在标题中留下了更笼统的描述(如果您对问题有更准确的描述,请发表评论,我会编辑标题)。 问题: AudioStream 和VideoStream 两个类派生自基类Medi
我的应用程序不仅具有“用户”,而且还具有“管理员”和“ super 管理员”。由于所有这三个共享相同的属性,因此我只想使用一个带有附加属性“role”的表,该属性可以是“user”,“admin”或“
我正在使用数据库优先方法和 DbContext。我的数据模型中有几个继承结构 (TPH)。但是 DbContext 只为基类创建一个 DbSet,而没有为子类创建一个 DbSet。我应该如何检索指定子
我正在使用数据库优先方法和 DbContext。我的数据模型中有几个继承结构 (TPH)。但是 DbContext 只为基类创建一个 DbSet,而没有为子类创建一个 DbSet。我应该如何检索指定子
我有一个生成很多子对象的应用程序,每个子对象都与一些全局应用程序对象一起工作,例如在全局应用程序注册表中注册自己,更新应用程序统计信息等。 应用程序应该如何将访问这些全局对象的能力传递给 child
我有 2 个结构,其中一个继承了由 type Common struct {...} 表示的所有结构中共有的值 type Common struct{ Id int CreatedAt
我的代码: Ext.onReady(function() { // Every property is declared inside the class Ext.define('MyCustomPa
我有一个包含多个类的应用程序,这些类继承/是第三方库类的子类。例如,来自 Qt Framework 的 QDialog。 我应该在应用程序的 UML 类图中指定继承关系吗? 是否期望(或标准)表示此类
我遇到了这个问题,因为我认为我对 Backbone 的理解不正确。 我有一个名为 Runnable 的父类(super class)和一组继承自它的子类: var Runnable = Backbon
考虑以下 HTML: foo bar 和 CSS: a { text-decoration:none; border-bottom-width: 0px; border-bot
我想做个模板Class BaseDialog ,但在进行最终对话时 MyDialog1 , 继承了 BaseDialog , 我收到错误,然后无法在设计模式下显示对话框。 以下是我得到的错误列表。 a
我对 android 文档中的这两个术语感到困惑!! these screenshot about what i mean 最佳答案 常量是用 static 和 final 修饰符声明的字段。继承常量
我最近遇到了一个新的警告: 继承构造函数不继承省略号 我正在尝试管道 Object{42}; // ... into an init that handles integers ...和... Obj
我正在使用 TypeScript 开发 Aurelia 应用程序。在此应用程序中,我定义了一组自定义元素,每个元素共享一组可绑定(bind)属性,这些属性被转换为 css 设置,如以下简化示例所示:
我想知道为什么人们会说: “继承类不继承构造函数”。 如果你可以使用父类的构造函数,无参构造函数无论如何都会被自动调用。 例子: #include using namespace std; clas
我刚刚开始探索 Kotlin 语言。我正在为继承、var&val 和副作用而苦苦挣扎。 如果我用 val x 声明一个特征 A 并在 AImpl 中覆盖 x ,则可以将其覆盖为 var(参见下面的代码
我正面临一个编码/解码问题,涉及使用 MOXy 的 JAXB 实现和外部元数据绑定(bind)文件的继承和多态性。 我无法控制 XML 文件或模型类。 模型内部有多个类继承其他 DTO 类。 这是我正
我是一名优秀的程序员,十分优秀!