最新消息:雨落星辰是一个专注网站SEO优化、网站SEO诊断、搜索引擎研究、网络营销推广、网站策划运营及站长类的自媒体原创博客

Type relationship via `extends` in TypeScript? - Stack Overflow

programmeradmin4浏览0评论

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
发布评论

评论列表(0)

  1. 暂无评论