Skip to content

Add TaggedUnion type#566

Merged
sindresorhus merged 2 commits intosindresorhus:mainfrom
pheis:feat/tagged-union-type
Mar 13, 2023
Merged

Add TaggedUnion type#566
sindresorhus merged 2 commits intosindresorhus:mainfrom
pheis:feat/tagged-union-type

Conversation

@pheis
Copy link
Copy Markdown
Contributor

@pheis pheis commented Mar 7, 2023

  • A type utility for creating a union of types that share a common discriminant property.

Example use case:

An union type like this

type EventMessage =
  | {
      type: 'OpenExternalUrl'
      url: string
    }
  | {
      type: 'ToggleBackButtonVisibility'
      visible: boolean
    }
  | {
      type: 'PurchaseButtonPressed'
      price: number
    }
  | {
      type: 'NavigationStateChanged'
      navigation?: string
    }

Can be expressed like this:

type Tagged<Fields extends Record<string, unknown> = TaggedUnion<'type', Fields>

type EventMessage = Tagged<{
    OpenExternalUrl: {
      url: string
    }
    ToggleBackButtonVisibility: {
      visible: boolean
    }
    PurchaseButtonPressed: {
      price: number
    }
    NavigationStateChanged: {
      navigation?: string
    }
}>

Comment thread source/tagged-union.d.ts Outdated
@@ -0,0 +1,41 @@
/**
Utility type to create a tagged union with given tag
Copy link
Copy Markdown
Owner

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

The first sentence in this file should be the same as the readme.

Copy link
Copy Markdown
Contributor Author

@pheis pheis Mar 8, 2023

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Good point! I fixed this :)

Comment thread source/tagged-union.d.ts Outdated
type EventMessage = TaggedUnion<
'tag',
{
NavigationStateChanged: {
Copy link
Copy Markdown
Owner

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Use tab-indentation.

Copy link
Copy Markdown
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I reformatted the comment with xo. Should have tabs now :)

Comment thread source/tagged-union.d.ts Outdated
```
import type {TaggedUnion} from 'type-fest';

type EventMessage = TaggedUnion<
Copy link
Copy Markdown
Owner

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I think it would be useful to show the same an example of the same just without this type. So you can clearly see how it simplifies making a tagged union.

Copy link
Copy Markdown
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

True! I added an example without use of this utility.

@pheis pheis force-pushed the feat/tagged-union-type branch from 10abdd4 to 91491b1 Compare March 8, 2023 11:33
@pheis pheis requested a review from sindresorhus March 8, 2023 11:38
@pheis pheis force-pushed the feat/tagged-union-type branch 3 times, most recently from 1b1805c to 8faf32d Compare March 13, 2023 06:56
pheis added 2 commits March 13, 2023 08:59
- use tab-intendation
- Add example to comment
- Improve doc comment
@pheis pheis force-pushed the feat/tagged-union-type branch from 8faf32d to 4b07661 Compare March 13, 2023 07:00
@sindresorhus sindresorhus merged commit 2e1cec8 into sindresorhus:main Mar 13, 2023
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.

3 participants