5

According to the definition of intersection, the intersection of several types should produce a type whose properties are common to all types. For example, if we have:

type o1 = {a: string, b: string}
type o2 = {a: string, c: string}
type o3 = o1 & o2

Then o3 should be a type which only have property a since a is the common property to o1 and o2. However, what we get actually is a type which have properties a, b and c. In this case, it seems the intersection type doesn't act like what its name has shown. It acts more like a combination of all types. So does intersection types really have something to do with intersection? If not, why we call it intersection types instead of combination types or other names?

Chor
  • 833
  • 1
  • 5
  • 14
  • No, `o3` will have `a, b, c`. – Dai Nov 25 '21 at 02:41
  • 1
    Type-theory uses terms like "union", "intersection", and "product" in a different way to set-theory... – Dai Nov 25 '21 at 02:42
  • 1
    Does this help? https://www.reddit.com/r/typescript/comments/9qduq3/why_is_type_intersection_called_like_that_its/ – ShamPooSham Nov 25 '21 at 02:56
  • @Dai not really, it's just that the "sets" you're probably thinking of are not the right ones. Don't think "set of properties"; think "set of values that belong to the type". By structural subtyping, the value `{a: "", b: "", c: ""}` is of type `o1` (which does not *prohibit* a `c` property) *and* of type `o2` (which does not *prohibit* a `b` property), so it is of both `o1` *and* `o2`, or `o1 & o2`, which is equivalent to `{a: string, b: string, c: string}`. – jcalz Nov 25 '21 at 03:13
  • @jcalz Right. I was writing up my answer that elaborates on that when you voted to close as a dupe. The dupe you linked to *does* answer the OP's question, but the significant part of your answer there is somewhat buried in the second paragraph. Whereas the answer I was writing was focused _only_ on that part - I'd like to post it, hence my reopen vote. – Dai Nov 25 '21 at 03:16
  • There's also https://stackoverflow.com/questions/66896430/why-do-typescript-intersections-behave-like-unions-only-on-objects – jcalz Nov 25 '21 at 03:18
  • @jcalz Welp, I just wasted the last 30 minutes... – Dai Nov 25 '21 at 03:18
  • We *could* reopen this one maybe, but there are quite a number of other nearly exact duplicates of this question, and the answer would be the same? One could presumably answer one of those as well as this one. Not sure what to do here, I understand the frustration when questions get closed out from under me when I'm writing an answer, but other than that, are you suggesting that this question isn't really a duplicate? – jcalz Nov 25 '21 at 03:20
  • @Dai I would be willing to vote to reopen if you can make the case that the question is not a duplicate (which would be a valid reason to reopen), as opposed to having your time wasted (which is certainly annoying but not a valid reason to reopen). – jcalz Nov 25 '21 at 03:23
  • @jcalz I'm a very self-entitled person :) – Dai Nov 25 '21 at 03:25
  • 1
    @jcalz Yes. Now I have figured it out. The key point here is to understand "what exactly is the set of type `o1`". That set actually includes all objects which have properties `a` and `b` but not only have `a` and `b`. For type `o2` this is also the same. In this case, `o1 & o1` should produce a type which have `a` and `b` and `c` since this type should satisfy both the features of `o1` and `o2`. – Chor Nov 26 '21 at 05:39

0 Answers0