Skip to content

feat: add Tagged for composable tagged types#672

Merged
sindresorhus merged 1 commit intosindresorhus:mainfrom
ethanresnick:main
Aug 26, 2023
Merged

feat: add Tagged for composable tagged types#672
sindresorhus merged 1 commit intosindresorhus:mainfrom
ethanresnick:main

Conversation

@ethanresnick
Copy link
Copy Markdown
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

@ethanresnick ethanresnick force-pushed the main branch 3 times, most recently from f4c7687 to 1b2a6bb Compare August 25, 2023 11:11
@sindresorhus
Copy link
Copy Markdown
Owner

This looks very good 👍

@sindresorhus
Copy link
Copy Markdown
Owner

You need to also add them to the readme.

@sindresorhus
Copy link
Copy Markdown
Owner

@ethanresnick ethanresnick force-pushed the main branch 3 times, most recently from 05069b4 to 3588e73 Compare August 25, 2023 20:32
@ethanresnick
Copy link
Copy Markdown
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
@sindresorhus
Copy link
Copy Markdown
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