Skip to content

Commit

Permalink
Allow data classes to be branded with an additional type parameter
Browse files Browse the repository at this point in the history
  • Loading branch information
mikearnaldi committed Nov 15, 2024
1 parent c138fa7 commit 68fe96b
Show file tree
Hide file tree
Showing 2 changed files with 34 additions and 4 deletions.
26 changes: 26 additions & 0 deletions .changeset/witty-balloons-shout.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,26 @@
---
"effect": minor
---

Allow data classes to be branded with an additional type parameter

```ts
import { Data, HashMap } from "effect"

export class T extends Data.Class<
{ id: string },
{ readonly _brand: unique symbol }
> {}

// type error
export const hashMap1: HashMap.HashMap<T, string> = HashMap.empty().pipe(
HashMap.set({ id: "one" }, "one"),
HashMap.set({ id: "one" }, "two")
)

// no type error
export const hashMap2: HashMap.HashMap<T, string> = HashMap.empty().pipe(
HashMap.set(new T({ id: "one" }), "one"),
HashMap.set(new T({ id: "one" }), "two")
)
```
12 changes: 8 additions & 4 deletions packages/effect/src/Data.ts
Original file line number Diff line number Diff line change
Expand Up @@ -183,10 +183,14 @@ export const tagged = <A extends { readonly _tag: string }>(
* @since 2.0.0
* @category constructors
*/
export const Class: new<A extends Record<string, any> = {}>(
args: Types.Equals<A, {}> extends true ? void
: { readonly [P in keyof A]: A[P] }
) => Readonly<A> = internal.Structural as any
export const Class: {
new<A extends Record<string, any> = {}>(
args: Types.Equals<A, {}> extends true ? void : { readonly [P in keyof A]: A[P] }
): Readonly<A>
new<A extends Record<string, any> = {}, Brand = {}>(
args: Types.Equals<A, {}> extends true ? void : { readonly [P in keyof A]: A[P] }
): Readonly<A> & Brand
} = internal.Structural as any

/**
* Provides a Tagged constructor for a Case Class.
Expand Down

0 comments on commit 68fe96b

Please sign in to comment.