Union Type 分配律

Created · Updated

type IsString<T> = T extends string ? "yes" : "no";

type T = IsString<"a" | 2 | "c">; // "yes" | "no"

Union Type 擁有分配率的特性:

type T = 
    | ("a" extends string ? "yes" : "no")
    | ( 2  extends string ? "yes" : "no")
    | ("c" extends string ? "yes" : "no");