[英]TypeScript - Distributive conditional types
Talk is cheap,show the code, By the way.说话很便宜,显示代码,顺便说一下。 the version of ts I am testing is 4.6.4
我测试的ts版本是4.6.4
type ITypeA = ((args: { A: any }) => any) | ((args: { B: any }) => any);
type Test<T> = T extends (args: infer A) => any ? A : never;
// type Result1 = {
// A: any;
// } | {
// B: any;
// }
type Result1 = Test<ITypeA>;
// type Result2 = {
// A: any;
// } & {
// B: any;
// }
type Result2 = ITypeA extends (args: infer A) => any ? A : never;
Result1
may use 'distributive conditional types' rule in ts, so type Result1 = { A: any;} | { B: any;}
Result1
可能在 ts 中使用 'distributive conditional types' 规则,所以type Result1 = { A: any;} | { B: any;}
type Result1 = { A: any;} | { B: any;}
. type Result1 = { A: any;} | { B: any;}
。
My question is why does Result2
not apply this rule?我的问题是为什么
Result2
不应用这条规则? Is there any difference between them?它们之间有什么区别吗?
When conditional types act on a generic type, they become distributive when given a union type.
当条件类型作用于泛型类型时,它们在给定联合类型时变得具有分配性。 For example, take the following:
例如,采用以下内容:
There is a requirement, you need act on generic
.有一个要求,你需要对
generic
采取行动。
Result2
acts on known types, there is no generic, whereas Result1
uses Test
which in turn uses generic T
Result2
作用于已知类型,没有泛型,而Result1
使用Test
,后者又使用generic T
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.