简体   繁体   English

Typescript 文字联合

[英]Typescript literal unions

I have two literal types, and I want to exclude B from union A.我有两种文字类型,我想从联合 A 中排除 B。

type A = 'a' | 'b'
type B = 'a'

When I do it like this:当我这样做时:

type MyExclude<A2, B2> = A2 extends B2 ? never : A2
type C2 = MyExclude<A, B>
// C2 = 'b'

It works correctly, ie i get 'b'.它工作正常,即我得到'b'。

But when I do it directly like this:但是当我直接这样做时:

type C = A extends B ? never : A
// C = 'a' | 'b'

It doesn't.它没有。 Why should it matter whether I do it via a intermediary generic type or directly?为什么我是通过中间泛型类型还是直接这样做很重要?

Thanks.谢谢。

When you do it as a generic, Typescript is going to apply the check distributively to each member of the union.当您将其作为通用执行时,Typescript 将把检查分配给工会的每个成员。 So type C2 = MyExclude<A, B> is going to be evaluated not as 'a | b' extends 'a'? never: 'a | 'b'所以type C2 = MyExclude<A, B>将被评估为不作为'a | b' extends 'a'? never: 'a | 'b' 'a | b' extends 'a'? never: 'a | 'b' 'a | b' extends 'a'? never: 'a | 'b' , but rather ('a' extends 'a'? never: 'a') | ('b' extends 'a'? never: 'b') 'a | b' extends 'a'? never: 'a | 'b' ,而是('a' extends 'a'? never: 'a') | ('b' extends 'a'? never: 'b') ('a' extends 'a'? never: 'a') | ('b' extends 'a'? never: 'b') . ('a' extends 'a'? never: 'a') | ('b' extends 'a'? never: 'b') 'a' does extend B, so that results in never . 'a' 确实扩展了 B,所以结果是never 'b' does not extend B, so that results in 'b'. 'b' 不扩展 B,因此结果为 'b'。 And the final result is never | 'b'而最终的结果never | 'b' never | 'b' , which is just 'b' . never | 'b' ,这只是'b'

The distributive part does not happen if it's not a generic.如果它不是通用的,则不会发生分配部分。 type C = A extends B? never: A type C = A extends B? never: A is just going to be evaluated once, as 'a | b' extends 'a'? never: 'a | 'b' type C = A extends B? never: A只被评估一次,作为'a | b' extends 'a'? never: 'a | 'b' 'a | b' extends 'a'? never: 'a | 'b' 'a | b' extends 'a'? never: 'a | 'b' . 'a | b' extends 'a'? never: 'a | 'b' 'a' | 'b' 'a' | 'b' does not extend 'a' , so the result is the whole type, 'a' | 'b' 'a' | 'b'不扩展'a' ,所以结果是整个类型, 'a' | 'b' 'a' | 'b'

See distributive conditional types查看分布式条件类型

Distributive conditional types分布式条件类型

Conditional types in which the checked type is a naked type parameter are called distributive conditional types.检查类型是裸类型参数的条件类型称为分布条件类型。 Distributive conditional types are automatically distributed over union types during instantiation.分布式条件类型在实例化期间自动分布在联合类型上。 For example, an instantiation of T extends U? X: Y例如, T extends U? X: Y T extends U? X: Y with the type argument A | B | C T extends U? X: Y类型参数A | B | C A | B | C A | B | C for T is resolved as (A extends U? X: Y) | (B extends U? X: Y) | (C extends U? X: Y) TA | B | C被解析为(A extends U? X: Y) | (B extends U? X: Y) | (C extends U? X: Y) (A extends U? X: Y) | (B extends U? X: Y) | (C extends U? X: Y) (A extends U? X: Y) | (B extends U? X: Y) | (C extends U? X: Y) . (A extends U? X: Y) | (B extends U? X: Y) | (C extends U? X: Y)

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

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