MutableClass

This commit is contained in:
Julien Valverdé
2024-06-10 03:20:01 +02:00
parent 5cba51ded4
commit 30e42b9dfd
4 changed files with 37 additions and 1 deletions

View File

@@ -0,0 +1,24 @@
import { Schema as S } from "@effect/schema"
import type { Annotations, Class, Struct } from "@effect/schema/Schema"
import type { HasFields, MissingSelfGeneric } from "./util"
export const MutableClass = <Self = never>(identifier: string) => (
<Fields extends Struct.Fields>(
fieldsOr: Fields | HasFields<Fields>,
annotations?: Annotations.Schema<Self>
): (
[Self] extends [never]
? MissingSelfGeneric<"Class">
: Class<
Self,
Fields,
Struct.Encoded<Fields>,
Struct.Context<Fields>,
Struct.Constructor<Fields>,
{},
{}
>
) =>
S.Class<Self>(identifier)(fieldsOr, annotations)
)

View File

@@ -1 +1 @@
export {}
export { MutableClass } from "./MutableClass"

View File

@@ -0,0 +1 @@
import { Schema as S } from "@effect/schema"

11
src/effect/schema/util.ts Normal file
View File

@@ -0,0 +1,11 @@
import type { Struct } from "@effect/schema/Schema"
export type MissingSelfGeneric<Usage extends string, Params extends string = ""> = (
`Missing \`Self\` generic - use \`class Self extends ${Usage}<Self>()(${Params}{ ... })\``
)
export type HasFields<Fields extends Struct.Fields> = (
| { readonly fields: Fields }
| { readonly from: HasFields<Fields> }
)