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

Allow data classes to be branded with an additional type parameter #3947

Draft
wants to merge 1 commit into
base: next-minor
Choose a base branch
from
Draft
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
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