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

Expose KeysOfUnion type #709

Merged
merged 5 commits into from Oct 17, 2023
Merged

Conversation

henriqueinonhe
Copy link
Contributor

Adds the AllKeys type, that is similiar to keyof but it distributes over unions, that is, it returns the keys of ALL members of a union type.

Although this is a first step to add the types in #132, it is also useful by itself.

Additionally, I only used this name (AllKeys) because it's what was suggested in the issue I mentioned above, but there are other possible names we might want to consider, like KeyOfDistributeUnions or DistributedKeyOf.

@sindresorhus
Copy link
Owner

You need to add it to the readme too.

@sindresorhus
Copy link
Owner

Some more naming alternatives:

  • AllMemberKeys
  • EveryKeyOf

@sindresorhus
Copy link
Owner

Hmm, we actually have a type for this already:

/**
Gets keys from a type. Similar to `keyof` but this one also works for union types.
The reason a simple `keyof Union` does not work is because `keyof` always returns the accessible keys of a type. In the case of a union, that will only be the common keys.
@link https://stackoverflow.com/a/49402091
*/
export type KeysOfUnion<T> = T extends T ? keyof T : never;

I think that name is the best.

@henriqueinonhe
Copy link
Contributor Author

Interesting... Would it be okay to expose this internal type then?

@sindresorhus
Copy link
Owner

sindresorhus commented Oct 16, 2023

You can just replace the use of that type with yours (make sure you merge any docs/tests for it with yours).

@henriqueinonhe henriqueinonhe force-pushed the add-all-keys branch 2 times, most recently from a52696c to b891b03 Compare October 17, 2023 12:25
@henriqueinonhe
Copy link
Contributor Author

Merged docs, replaced uses.
There were no preexisting tests for this specific type, so I left the ones I added.

@henriqueinonhe henriqueinonhe changed the title Add AllKeys type Expose UnionAllKeys type Oct 17, 2023
@henriqueinonhe henriqueinonhe changed the title Expose UnionAllKeys type Expose KeysOfUnion type Oct 17, 2023
@sindresorhus sindresorhus merged commit 0517399 into sindresorhus:main Oct 17, 2023
6 checks passed
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

Successfully merging this pull request may close these issues.

None yet

2 participants