#why is it called intersection type (& operator) in typescript, when it literally mashes the types?

10 messages · Page 1 of 1 (latest)

placid mist

Intersection types in TypeScript can be described using the & (ampersand) operator.

For example

function makeWeek(): Date & { end: Date } {
  //⬅ return type
 
  const start = new Date()
  const end = new Date(start.valueOf() + ONE_WEEK)
 
  return { ...start, end } // kind of Object.assign
}
 
const thisWeek = makeWeek()
thisWeek.toISOString()
thisWeek.end.toISOString()

we can access,
thisWeek.(DateObjFuncs) and this.Week.end.(DateObjFuncs)
Which means we can access the UNION of the types, confused about the (& operator) being called Intersection Type
i am trying to relate this to set theory, intersection means the common..

https://www.typescriptlang.org/play?#code/MYewdgzgLgBA8gOQKIH0DqSkGkYF4YCMADCTAFQwBsR5VNFATACy0DsMA9B4QO4wCWYGAFsIAKC7cAtDOABXKDKliAZnLDAo-cCICGAawCmaQ4f0AKAJQAuGABFdUQzABkMAN4xDYACa2HTjAAvh5iMJwcgKDUMABOhlByMUJQAJ4ADoZiYTCgkLDQujGw+GCGfAGGVtm50F6+eDCl5Y6VBUUAdABuugA2coZwKlYwANTwyOiYWJZZ4XEJSR4w7SttUAA0dT7BMGJBWTWwUAAW-BAmZg3CBsamFjNiJ2cX+u1QIACSAMpwX1AxggA5sNHqdznd2t4fG9Pj8-gCwMDLEA
leaving a typescript playground link of the same code being talked of

||this code is from a course I am doing||

ancient kiln

Because those types mean that both properties need to exist. Since objects can have additional properties and still fulfill the type what you have here is a type that will only be correct if the value belongs to both types at the same time (hence part of the intersection of those types in a set theory sense)

For the same reason the union type | is called union

tepid ivy

As quoted in the handbook™️ https://www.typescriptlang.org/docs/handbook/2/everyday-types.html#union-types

It might be confusing that a union of types appears to have the intersection of those types’ properties. This is not an accident - the name union comes from type theory. The union number | string is composed by taking the union of the values from each type. Notice that given two sets with corresponding facts about each set, only the intersection of those facts applies to the union of the sets themselves. For example, if we had a room of tall people wearing hats, and another room of Spanish speakers wearing hats, after combining those rooms, the only thing we know about every person is that they must be wearing a hat.

though i really don't find that example very good fwiw

ancient kiln

You also know that they must be either Spanish or tall (or both) but they omit that information on purpose there

warm glade

Which means we can access the UNION of the types, confused about the (& operator) being called Intersection Type
this is incorrect

typescript is based on sets as qjuh mentioned
types are sets, and values are elements of those sets
unknown is the universe set, never is the empty set
string is the set of all strings, a subset of unknown
string | number is a set unioned from the set of strings and set of numbers
so valid values can be either strings or numbers
{ a: string } and { b: string } overlap at { a: string; b: string }, so that is the result of { a: string } & { b: string }

placid mist
ancient kiln