I started playing with the example code shown at 5:45 and 6:19 and found it to be the opposite. Intellisense gives me access to all methods when using `Cat & Dog`, but only access to the shared `eat()` method when using `Cat | Dog`. Now I'm really confused. The presenter's logic makes perfect sense, but Typescript's compiler is telling me otherwise. Can someone please explain?
@heikoschmale Жыл бұрын
Well paid attention. It's just the other way round. Think of union as discriminated unions. Thus on 'Cat | Dog' (read: EITHER cat OR dog), only 'eat()' can be applied safely. It can't be both at the SAME time. On the other side, the intersection 'Cat & Dog' returns a new type with all the properties of 'Cat' and all the properties of 'Dog', because 'Cat & Dog' is both a subset of 'Cat' and a subset of 'Dog'. Thus 'Cat & Dog' can BEHAVE like a 'Cat' and a 'Dog' at the SAME time.
@ivanovejero8421 Жыл бұрын
Thanks for raising this. You are of course correct. A union of two interfaces produces a smaller interface (fewer methods), which stands for a larger set - membership there only requires the elements in common from both input sets. An intersection of two interfaces produces a larger interface (more methods), which stands for a smaller set - membership there requires all elements from both input sets.
@phoehtaung3 жыл бұрын
using set-theory ideas for types is super helpful!
@dayumnson97693 жыл бұрын
wow, great talk! Give this man a medal! Btw. ivov, did yiu ever try out haskells type system?
@ivanovejero84213 жыл бұрын
Thank you! Haskell is on my list to learn.
@heavierthanlight71732 жыл бұрын
sooooo... "combined" and "shared" would be better names for "unions" and "intersection" exactly how a database uses it.
@yassinebouchoucha2 жыл бұрын
Math is the fundamental of Computer Science not Data Structure and algorithm,
@carloslfu2 жыл бұрын
I find TypeScript design a bit flawed in this aspect. It is not what people expect from those operations and it makes trivial stuff non-trivial.