Skip to content

Commit

Permalink
add Data.TaggedEnum.WithGenerics example
Browse files Browse the repository at this point in the history
  • Loading branch information
gcanti committed Dec 6, 2023
1 parent 32ad2fc commit 800e224
Show file tree
Hide file tree
Showing 2 changed files with 29 additions and 1 deletion.
8 changes: 7 additions & 1 deletion pages/docs/data-types/data.mdx
Original file line number Diff line number Diff line change
Expand Up @@ -6,7 +6,7 @@ The Data module offers a range of features that make it easier to create and man

If you need to compare existing values for equality without the need for explicit
implementations, consider using the Data module. It provides convenient APIs
that generate default implementations for [`Equal`](../trait/equal) and [`Hash`](../trait/hash), making equality
that generate default implementations for [`Equal`](../trait/equal.mdx) and [`Hash`](../trait/hash.mdx), making equality
checks a breeze.

### struct
Expand Down Expand Up @@ -116,3 +116,9 @@ Let's walk through an example:
```

Note that it follows the convention within the Effect ecosystem of naming the tag field with `"_tag"`.

You can also pass a `TaggedEnum.WithGenerics` if you want to add generics to the constructors:

```ts file=<rootDir>/src/data/TaggedEnumWithGenerics.ts

```
22 changes: 22 additions & 0 deletions src/data/TaggedEnumWithGenerics.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,22 @@
import { Data } from "effect"

type RemoteData<A, B> = Data.TaggedEnum<{
Loading: {}
Success: { data: A }
Failure: { error: B }
}>

interface RemoteDataDefinition extends Data.TaggedEnum.WithGenerics<2> {
readonly taggedEnum: RemoteData<this["A"], this["B"]>
}

const { Loading, Failure, Success } = Data.taggedEnum<RemoteDataDefinition>()

// $ExpectType Data<{ readonly _tag: "Loading"; }>
const loading = Loading()

// $ExpectType Data<{ readonly _tag: "Failure"; readonly error: string; }>
const failure = Failure({ error: "err" })

// $ExpectType Data<{ readonly _tag: "Success"; readonly data: number; }>
const success = Success({ data: 1 })

0 comments on commit 800e224

Please sign in to comment.