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

DOCS: wrong/confusing explanation of union of objects #3289

Closed
melogo5 opened this issue Dec 23, 2024 · 1 comment
Closed

DOCS: wrong/confusing explanation of union of objects #3289

melogo5 opened this issue Dec 23, 2024 · 1 comment

Comments

@melogo5
Copy link

melogo5 commented Dec 23, 2024

Page URL: https://www.typescriptlang.org/docs/handbook/2/everyday-types.html#union-types

Issue: wrong/confusing explanation of union of objects.
Info block at the end of "Union Types" section states:

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.

Example of code dependant on 2 principles from the explanation:

a union of types appears to have the intersection of those types’ properties>

the only thing we know about every person is that they must be wearing a hat>

Image

But it gives us an error:
Image

So, working example for the provided explanation look like this. But i see no connection between following code and provided explanation in doc:
Image

Proposals:
Edit info block in doc or remove it

Simillar topic about how union types actually works microsoft/TypeScript#20863

@jakebailey
Copy link
Member

You're thinking about this backwards; "a union of types appears to have the intersection of those types’ properties" refers to accessing properties, not constructing them. When you union those two types together, the only property you can access is wearingHats.

@jakebailey jakebailey closed this as not planned Won't fix, can't repro, duplicate, stale Jan 7, 2025
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

No branches or pull requests

2 participants