[英]TypeScript 3.9.5- The intersection '…' was reduced to 'never' because property 'usage' has conflicting types in some constituents
I have a function which take in a class definition and uses that to return a new abstract class.我有一个 function 接受 class 定义并使用它返回一个新的抽象 class。 Up to TS v3.8.3 this worked fine.直到 TS v3.8.3 这工作得很好。 Now while upgrading to TS v3.9.5 Typescript appears to be getting a different type from the class passed into the function.现在,在升级到 TS v3.9.5 时,Typescript 似乎得到了与传递给 function 的 class 不同的类型。 It's best if I just reproduce the code here.最好是在这里重现代码。
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);
}
}
The error from Typescript is highlighted on AttributeBase(AttributeModel)
.来自 Typescript 的错误在AttributeBase(AttributeModel)
上突出显示。 In this example AttributeModel
is being passed into the function as the Base
argument.在此示例中, AttributeModel
被传递到 function 作为Base
参数。 If we use AttributeModel
directly inside the function instead of the Base
argument the error goes away.如果我们直接在 function 中使用AttributeModel
而不是Base
参数,错误就会消失。 But this is the same thing, right?但这是同一回事,对吧?
I'm not an expert in Typescript yet, but we do need some way to use this AttributeBase
function, or some other method, to dynamically extend multiple base classes.我还不是 Typescript 方面的专家,但我们确实需要一些方法来使用这个AttributeBase
function 或其他方法来动态扩展多个基类。 Perhaps the answer is obvious, but this does seem like rather odd behavior.也许答案是显而易见的,但这似乎是相当奇怪的行为。 I've tried a bunch of other things but nothing that maintains the same type strictness we get from this setup.我已经尝试了很多其他的东西,但没有任何东西能保持我们从这个设置中获得的相同类型的严格性。
So can someone please explain to me what's going on here and how I might fix this so we get the same type safety without the never
return type from AttributeBase(AttributeModel)
?那么有人可以向我解释这里发生了什么以及我如何解决这个问题,以便我们在没有AttributeBase(AttributeModel)
never
返回类型的情况下获得相同的类型安全性?
I've also posted this code to a repo here if that helps.如果有帮助,我也会将此代码发布到 此处的存储库中。
Problem is that your BufferAttributeUsageModel
does not extend AttributeUsageModel
.问题是您的BufferAttributeUsageModel
没有扩展AttributeUsageModel
。
Your AttributeBaseModel<T extends AttributeUsageModel>
requires T extends AttributeUsageModel
, but then you define class AttributeModel extends AttributeBaseModel<BufferAttributeUsageModel>
with BufferAttributeUsageModel
that does not extend AttributeUsageModel
.您的AttributeBaseModel<T extends AttributeUsageModel>
需要T extends AttributeUsageModel
,但随后您使用不扩展AttributeUsageModel
的BufferAttributeUsageModel
定义class AttributeModel extends AttributeBaseModel<BufferAttributeUsageModel>
。 That's where your usage
property "has conflicting types in some constituents."这就是您的usage
属性“在某些成分中具有冲突类型”的地方。 - as the error states. - 如错误所述。
To fix that, you can define BufferAttributeUsageModel
as:要解决这个问题,您可以将BufferAttributeUsageModel
定义为:
type BufferAttributeUsageModel =
AttributeUsageModel
| 0x81
| 0x90
| 0xf0
| 0xfa
| 0xfb
| 0xfc
| 0xfd
| 0xfe
| 0xff;
Perhaps, you should try to simplify those type definitions.也许,您应该尝试简化这些类型定义。
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.