简体   繁体   English

Union-Type 变成 Intersection-Type

[英]Union-Type turns into Intersection-Type

Problem问题

I want to create a factory that delivers different functions depending on the given parameters.我想创建一个根据给定参数提供不同功能的工厂。 This can be solved with conditional types.这可以通过条件类型来解决。

type DispatchConditional<TPayload> = TPayload extends undefined
  ? () => void
  : (payload: TPayload) => void;

The type DispatchCondition can be instrumented by an interface. DispatchCondition类型可以通过接口进行检测。

interface ActionDispatcher<TPayload> {
  dispatch: DispatchConditional<TPayload>;
}

If I build a factory based on the definitions above, it will break if a Union-Type is used.如果我根据上面的定义建立一个工厂,如果使用Union-Type ,它就会中断。 Somehow the union-type defined by payloadUnion() becomes an Intersection-Type .不知何故,由payloadUnion()定义的联合类型变成了Intersection-Type

在此处输入图像描述

Question问题

What adjustments do I have to make to produce a type definition for the method dispatch that supports union-types?我必须进行哪些调整才能为支持联合类型的方法dispatch生成类型定义?

Playground操场

DispatchConditional is a distributed conditional type . DispatchConditional是一种分布式条件类型 This means that DispatchConditional<number | number[]>这意味着DispatchConditional<number | number[]> DispatchConditional<number | number[]> is equivalent to DispatchConditional<number> | DispatchConditional<number[]> DispatchConditional<number | number[]>等价于DispatchConditional<number> | DispatchConditional<number[]> DispatchConditional<number> | DispatchConditional<number[]> which in turn will be a union of functions. DispatchConditional<number> | DispatchConditional<number[]>这又将是函数的联合。 And a union of functions is only callable with an intersection of the parameter type (since we don't know which function we actually get at runtime, we have to provide parameters that work with BOTH function signatures, resulting in the intersection)并且函数的联合只能通过参数类型的交集来调用(因为我们不知道在运行时我们实际上得到了哪个 function,我们必须提供与 BOTH function 签名一起使用的参数,从而导致交集)

The simple solution is to disable the distributive behavior, by wrapping the type parameter in a tuple type ( distribution only happens over naked type parameters):简单的解决方案是禁用分配行为,通过将类型参数包装在元组类型中(分配只发生在裸类型参数上):


type DispatchConditional<TPayload> = [TPayload] extends [undefined]
  ? () => void
  : (payload: TPayload) => void;

Playground Link 游乐场链接

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

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM