简体   繁体   English

在这个 TypeScript 示例中,如何解决“X 被简化为‘never’,因为属性‘Y’在某些成分中具有冲突类型”?

[英]How to resolve "X was reduced to 'never' because property 'Y' has conflicting types in some constituents" in this TypeScript example?

I have a fairly large compiler project I'm working on, and have copied out the relevant TypeScript portions into this TypeScript playground (it's a lot of types, which I think might be part of the problem).我有一个相当大的编译器项目,我正在处理,并将相关的 TypeScript 部分复制到这个 TypeScript 游乐场(它有很多类型,我认为这可能是问题的一部分)。

The function at the top is this, which is throwing an error:顶部的 function 是这样的,它会引发错误:

export function createNest<T extends Nest>(
  like: T,
  scope: SiteStepScopeType,
): NestType<T> {
  return {
    children: [],
    like,
    scope,
  }
}

It is throwing:它正在投掷:

Type '{ children: never[]; like: Nest; scope: SiteStepScopeType; }' is not assignable to type 'NestType<T>'.
  Type '{ children: never[]; like: Nest; scope: SiteStepScopeType; }' is not assignable to type 'never'.
    The intersection 'NestBaseType & { like: Nest.Assertion; } & { like: Nest.Bind; } & SiteModuleBaseType & { like: Nest.BookModule; } & { like: Nest.Boolean; } & { like: Nest.BorrowVariable; } & ... 31 more ... & { ...; }' was reduced to 'never' because property 'like' has conflicting types in some constituents.(2322)

Basically:基本上:

...was reduced to 'never' because property 'like' has conflicting types in some constituents ...被简化为“从不”,因为属性“like”在某些成分中具有冲突的类型

I checked like on the Nest* types, and they all have a relevant value, I can't see anything that could be wrong. like检查了Nest*类型,它们都有一个相关值,我看不出有什么可能是错误的。 Is it because I have so many types?是因为我的种类多吗? Or what is it exactly that's wrong?或者到底错在哪里?

The error message is rather confusing, but the problem the compiler doesn't understand the case where one of the values in Nest is not a key of the NestMappingType type.错误消息相当混乱,但问题是编译器不理解Nest中的值之一不是NestMappingType类型的键的情况。 To my knowledge, there is no way to specify this relationship, but you can inform the compiler what's happening by using an additional constraint on T in createNest and a type assertion, like so;据我所知,没有办法指定这种关系,但是您可以通过在createNest中对T使用附加约束和类型断言来通知编译器发生了什么,就像这样;

export function createNest<T extends keyof NestMappingType & Nest>(
  like: T,
  scope: SiteStepScopeType,
): NestType<T> {
  return {
    children: [],
    like,
    scope,
  } as NestType<T>
}

If you prefer, instead of the additional type constraint you could also redefine Nest like this:如果你愿意,你也可以像这样重新定义Nest ,而不是额外的类型约束:

enum NestMappingKey {
  Assertion = "nest-assertion",
  Bind = "nest-bind",
}

export type Nest = NestMappingKey & keyof NestMappingType;

暂无
暂无

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

相关问题 交集 '... &...' 被缩减为 'never' 因为属性 '...' 在某些成分中具有冲突的类型 - The intersection '... & ...' was reduced to 'never' because property '...' has conflicting types in some constituents TypeScript 3.9.5- 交叉点“...”被简化为“从不”,因为属性“使用”在某些成分中有冲突的类型 - TypeScript 3.9.5- The intersection '…' was reduced to 'never' because property 'usage' 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 交集 &#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 Typescript 映射返回类型:交集减少为从不,交集属性冲突 - Typescript mapped return type: intersection reduced to never, conflicting intersection property 打字稿:“ X”不可分配给类型,属性“ Y”的类型不兼容 - Typescript: “X” is not assignable to type, types of property “Y” are incompatible TypeScript 参数交集减少为从不 - TypeScript parameters intersection 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 运算符&#39;==&#39;不能应用于Typescript 2中的类型x和y - Operator '==' cannot be applied to types x and y in Typescript 2 React TypeScript:属性“ X”的类型不兼容。 类型“ Y”不可分配给类型“ Z” - React TypeScript: Types of property 'X' are incompatible. Type 'Y' is not assignable to type 'Z'
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM