[英]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.