Typescript 键入脚本3.9.5-交叉点'';减少到';绝不';因为财产';用法';在某些成分中存在冲突类型
我有一个函数,它接受一个类定义并使用它返回一个新的抽象类。在TS v3.8.3之前,该功能运行良好。现在,在升级到TS v3.9.5时,Typescript似乎从传递到函数中的类中获得了不同的类型。最好是在这里复制代码Typescript 键入脚本3.9.5-交叉点'';减少到';绝不';因为财产';用法';在某些成分中存在冲突类型,typescript,Typescript,我有一个函数,它接受一个类定义并使用它返回一个新的抽象类。在TS v3.8.3之前,该功能运行良好。现在,在升级到TS v3.9.5时,Typescript似乎从传递到函数中的类中获得了不同的类型。最好是在这里复制代码 enum AttributeUsageModel { ContractHash = 0x00, ECDH02 = 0x02, ECDH03 = 0x03, Script = 0x20, Vote = 0x30, DescriptionUrl = 0x81,
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);
}
}
enum attributeusage模型{
ContractHash=0x00,
ECDH02=0x02,
ECDH03=0x03,
脚本=0x20,
投票=0x30,
DescriptionUrl=0x81,
Description=0x90,
Hash1=0xa1,
Hash2=0xa2,
Hash3=0xa3,
Hash4=0xa4,
Hash5=0xa5,
}
类型BufferAttributeUsageModel=
|0x81
|0x90
|0xf0
|0xfa
|0xfb
|0xfc
|0xfd
|0xfe
|0xff;
类型构造函数=new(…参数:any[])=>T;
抽象类AttributeBaseModel{
公共摘要只读用法:T;
}
类AttributeModel扩展了AttributeBaseModel{
公共只读用法:BufferAttributeUsage模型;
公共构造函数(用法:BufferAttributeUsageModel){
超级();
用法=用法;
}
}
函数属性库<
使用扩展了属性模型,
t如果有帮助,数据库将扩展构造函数。问题是您的BufferAttributeUsageModel
没有扩展AttributeUsageModel
您的attributeAsseModel
需要T扩展AttributeUsageModel
,但随后您使用不扩展AttributeUsageModel
的BufferAttributeUsageModel
来定义类AttributeModel扩展attributeAsseModel
。这就是您的用法
属性“在某些成分中存在冲突类型”的原因所在
要解决此问题,您可以将BufferAttributeUsageModel
定义为:
类型BufferAttributeUsageModel=
属性模型
|0x81
|0x90
|0xf0
|0xfa
|0xfb
|0xfc
|0xfd
|0xfe
|0xff;
也许,您应该尝试简化这些类型定义。谢谢!这就是解决我的问题。BufferAttributeUsageModel
中的数字都包含在enum
AttributeUsageModel
中,因此我可以通过将BufferAttributeUsageModel
定义为AttributeUsageModel
enum的必要成员之间的联合来保持更严格的类型安全性(在其他地方也可以这样做)。把它写出来给其他发现它的人。