As far as I understand A extends B
implies A
is "at least within" B
. So for unions A
extends B
means at least one case of B
is satisfied by A
and A
has no cases outside of B
, and for products it means that A
has at least the properties of B
.
If that is correct, then I don't understand the following:
function doesExtend<A, B>(_: (A extends B ? 'yes' : 'no')) { }
doesExtend<'a', 'a'>('yes'); // correct: yes, identical
doesExtend<'a' | 'b', 'a' | 'b'>('yes'); // correct: yes, still identical
doesExtend<'b' | 'a', 'a' | 'b'>('yes'); // correct: yes, still identical
doesExtend<'a' | 'b', 'a' | 'b'>('no'); // correct: no, because they are idential right?
doesExtend<'a', 'a' | 'b'>('yes'); // correct: yes, subset is within a superset
doesExtend<'a' | 'b', 'a'>('no'); // correct: no, superset is larger than a subset
doesExtend<'a' | 'b', 'a'>('yes'); // <--- HERE! wtf? (incorrect yes)
Now, to make it even worse:
type WhoExtendsWho<A, B> = A extends B
? B extends A
? 'A extends B, and B extends A'
: 'A extends B, but B does not extend A'
: B extends A
? 'A does not extend B, but B extends A'
: 'A does not extend B, and B does not extend A';
type X = WhoExtendsWho<'a' | 'b', 'a'>; // "A extends B, and B extends A" | "A does not extend B, and B does not extend A"
So my question is how can binary dichotomy give me these 2 answers?
UPDATE:
- question answered, the solution is in the comments