I wanted to create a simple NOT operator in typescript where you get all primitives combined into the union of some type A that are NOT primitive members of the union of a second type B. This can be done using conditional types. For example, if you have types:
type A = 'a' | 'b' | 'c';
type B = 'c' | 'd' | 'e';
... then I want to map them to a third derived type [A - B] that, in this case, would yield:
type C = 'a' | 'b'
This seems to be doable using conditionals of the form shown below. HOWEVER, I am completely stumped why the NOT operator below seems to give me what I want, but explicitly spelling out the exact same conditional logic does not:
type not_A_B_1 = A extends B ? never : A; // 'a' | 'b' | 'c'
type Not<T, U> = T extends U ? never : T;
type not_A_B_2 = Not<A, B> // 'a' | 'b'
See here.
Could someone pls tell me if I'm missing some TS subtlety here that would explain why not_A_B_1
and not_A_B_2
are not equivalent? Thanks.