【发布时间】:2020-10-01 21:23:58
【问题描述】:
我有一个函数,它接受一个类定义并使用它来返回一个新的抽象类。直到 TS v3.8.3 这工作得很好。现在,在升级到 TS v3.9.5 时,Typescript 似乎获得了与传递给函数的类不同的类型。最好是把代码复制到这里。
enum AttributeUsageModel {
ContractHash = 0x00,
ECDH02 = 0x02,
ECDH03 = 0x03,
Script = 0x20,
Vote = 0x30,
DescriptionUrl = 0x81,
Description = 0x90,
Hash1 = 0xa1,
Hash2 = 0xa2,
Hash3 = 0xa3,
Hash4 = 0xa4,
Hash5 = 0xa5,
}
type BufferAttributeUsageModel =
| 0x81
| 0x90
| 0xf0
| 0xfa
| 0xfb
| 0xfc
| 0xfd
| 0xfe
| 0xff;
type Constructor<T> = new (...args: any[]) => T;
abstract class AttributeBaseModel<T extends AttributeUsageModel> {
public abstract readonly usage: T;
}
class AttributeModel extends AttributeBaseModel<BufferAttributeUsageModel> {
public readonly usage: BufferAttributeUsageModel;
public constructor(usage: BufferAttributeUsageModel) {
super();
this.usage = usage;
}
}
function AttributeBase<
Usage extends AttributeUsageModel,
TBase extends Constructor<AttributeBaseModel<Usage>>
>(Base: TBase) {
// Replacing `Base` with `AttributeModel` apparently fixes it
abstract class AttributeBaseClass extends Base {} // Base constructor return type is apparently 'never'
return AttributeBaseClass;
}
// Base constructor return type 'never' is not an object type or intersection of object types with
// statically known members.
// The intersection 'AttributeBase<AttributeUsageModel, typeof
// AttributeModel>.AttributeBaseClass & AttributeModel' was
// reduced to 'never' because property 'usage' has conflicting types in some constituents.
class BufferAttribute extends AttributeBase(AttributeModel) {
public constructor(usage: BufferAttributeUsageModel) {
super(usage);
}
}
来自 Typescript 的错误在 AttributeBase(AttributeModel) 上突出显示。在此示例中,AttributeModel 作为 Base 参数传递给函数。如果我们直接在函数内部使用AttributeModel 而不是Base 参数,错误就会消失。但这是一回事,对吧?
我还不是 Typescript 方面的专家,但我们确实需要某种方法来使用此 AttributeBase 函数或其他方法来动态扩展多个基类。也许答案是显而易见的,但这似乎是相当奇怪的行为。我尝试了很多其他的东西,但没有任何东西能保持我们从这个设置中获得的相同类型严格性。
那么有人可以向我解释这里发生了什么以及我如何解决这个问题,以便我们在没有来自AttributeBase(AttributeModel) 的never 返回类型的情况下获得相同的类型安全?
如果有帮助,我也已将此代码发布到 repo here。
【问题讨论】:
标签: typescript