Distributive Conditional Types
例えば、 T
が、 A|B|C
だとすると、
直感的には、 (A|B|C extends U) ? X : Y
になりそうだが、
実際は、 (A extends U ? X : Y) | (B extends U ? X : Y) | (C extends U ? X : Y)
になる
以下のことにも注意
tstype Exclude<T, U> = T extends U ? never : T;
ここで、第1引数、第2引数に下記を使うことを考える
tstype T1 = 'a' | 'b' | 'c';
type U1 = 'a' | 'b';
tstype A = T1 extends U1 ? never : T1; // 'a' | 'b' | 'c'
tstype B = Exclude_<T1, U1>; // 'c'
tstype B1 = Exclude_<T1, U1>;
// T1を代入
type B2 = Exclude_<'a' | 'b' | 'c', U1>;
// 分配!
type B3 = Exclude_<'a', U1> | Exclude_<'b', U1> | Exclude_<'c', U1>;
// 個々で結果が得られる
type B4 = never | never | 'c';
// 結果
type B5 = 'c';