Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Union vs. intersection types #295

Open
nyfarn opened this issue Mar 31, 2019 · 7 comments
Open

Union vs. intersection types #295

nyfarn opened this issue Mar 31, 2019 · 7 comments
Labels
bug v2 Workshop v2

Comments

@nyfarn
Copy link

nyfarn commented Mar 31, 2019

Is your feature request related to a problem? Please describe.
If I'm not mistaken, the Basics code example under "Intersection types" actually shows a union type and vice versa.
Cf. https://www.typescriptlang.org/docs/handbook/advanced-types.html

Describe the solution you'd like
The headings could be swapped.

Thank you for this great course.

@mike-north
Copy link
Member

Good catch! unfortunately I can't change the notes since we've already recorded the course and can't allow the video and code to get out of sync. Definitely something to ensure we get right in the next version!

@mike-north mike-north added v2 Workshop v2 and removed v3 labels Apr 1, 2019
@nyfarn nyfarn changed the title Union vs. interface types Union vs. intersection types Apr 1, 2019
@nyfarn
Copy link
Author

nyfarn commented Apr 1, 2019

Sure, that makes sense. Thank you!

@umar-khan
Copy link

Would it be possible to have a note saying that the Intersection and Union notes should be swapped? It might help future students at least until a v3 is up.

I've seen other courses have little helpful popups which show up as blue sections on the video timeline. Eg.

image

@hatched-esther
Copy link

Wow.. Im here because I was like... this does not make sense hahah glad to find that Im not crazy.

@paseaf
Copy link

paseaf commented Aug 19, 2020

Actually I find typescript's naming of Union and Intersection types very confusing... I haven't yet found any good explanation of why they are named like that :c

@nyfarn
Copy link
Author

nyfarn commented Aug 19, 2020

@paseaf You can think of types as sets of values. The type boolean is the set consisting of the values true and false. The literal type "foo" is the set that only contains that string. Now the union type boolean | "foo" is exactly the union of those two previous sets, a set that consists of those three values. Similarly, boolean & "foo" would be the intersection, the empty set in this case, called never in TS.

Now if you talk about how you can use those types then you get the opposite result. The properties that you can access on a union type is the intersection of the properties of the individual types. So boolean | "foo" has a toString method because both booleans and strings have it. And the properties that you can access on an intersection type is the union of the properties of the individual types. I'm guessing this is what makes these names confusing.

@paseaf
Copy link

paseaf commented Aug 19, 2020

The properties that you can access on a union type is the intersection of the properties of the individual types.

@nyfarn That's exactly what I was missing! Thank you so much!

I just realized it's another way to think of things. The Union and Intersection are operating on properties of the involved types, and the result are the properties of the result types. This post has also helped me after reading it for a few times.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
bug v2 Workshop v2
Projects
None yet
Development

No branches or pull requests

5 participants