简体   繁体   English

TypeScript 3.9.5- 交叉点“...”被简化为“从不”,因为属性“使用”在某些成分中有冲突的类型

[英]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 ,但随后您使用不扩展AttributeUsageModelBufferAttributeUsageModel定义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.

相关问题 交集 &#39;xxx &amp; xxx&#39; 被简化为 &#39;never&#39; 因为属性 &#39;xxx&#39; 在某些成分中具有冲突类型 - The intersection 'xxx & xxx' was reduced to 'never' because property 'xxx' has conflicting types in some constituents 类型联合被简化为“从不”打断打字:类型被简化为“从不”,因为属性在某些成分中具有冲突类型 - Union of types was reduced to 'never' breaks typing: Type was reduced to 'never' because property has conflicting types in some constituents Typescript 映射返回类型:交集减少为从不,交集属性冲突 - Typescript mapped return type: intersection reduced to never, conflicting intersection property TypeScript 参数交集减少为从不 - TypeScript parameters intersection reduced to never 交叉点“……”被缩减为从不 - The intersection '…' was reduced to never Typescript function参数有两种类型,一种类型的属性不能访问,因为它不是另一种类型的属性 - Typescript function parameter has two types, and the property of one type can't be accessed because it is not a property of the other type TypeScript相交类型 - TypeScript intersection types 与 Typescript 的交叉点类型 - Intersection types with Typescript typescript 交叉口类型 function - typescript intersection types of function 为什么交集类型包含冲突类型? - Why can intersection types contain conflicting types?
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM