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

feat: add Tagged for composable tagged types #672

Merged
merged 1 commit into from
Aug 26, 2023

Conversation

ethanresnick
Copy link
Contributor

@ethanresnick ethanresnick commented Aug 25, 2023

This adds two new types, Tagged and UnwrapTagged, per #665.

It also tweaks UnwrapOpaque so that it works on types produced by Tagged (as well as still working on types produced by Opaque, of course). This is a small gesture at interoperability between Opaque and Tagged, to make it easier for codebases to migrate from Opaque to Tagged.

Fixes #665

source/opaque.d.ts Outdated Show resolved Hide resolved
@sindresorhus
Copy link
Owner

This looks very good 👍

@sindresorhus
Copy link
Owner

You need to also add them to the readme.

@ethanresnick
Copy link
Contributor Author

ethanresnick commented Aug 25, 2023

Ok, I think I addressed all the comments. For the three types you mentioned that use Opaque (Exact, InvariantOf, and ConditionalPickDeep), I did something different in each case:

  • Exact: I extended the current approach to work with Tagged types too. As I mentioned in the inline code comment, I'm not sure the current approach is exactly correct/ideal, but that can be a topic for a separate PR.

  • InvariantOf: I kept the same basic logic, but inlined it to remove the dependency on Opaque, in case we want to deprecate Opaque at some point. I also put the tag that's enforcing the invariance in a differently-named symbol, so that it won't conflict with tags added by Tagged/Opaque. (Before, it was impossible to do Opaque<InvariantOf<X>> or InvariantOf<Opaque<X>>, as both InvariantOf and Opaque would've tried to put conflicting types at the same phantom [tag] key.)

  • ConditionalPickDeep didn't seem like it should be using opaque/tagged types at all; from my reading, it looks like it really just wanted a unique symbol type. So, I updated that accordingly, and all the tests still pass.

@sindresorhus sindresorhus merged commit 277ada2 into sindresorhus:main Aug 26, 2023
5 checks passed
@sindresorhus
Copy link
Owner

Thanks ;)

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.

Support composable tagged types
2 participants