TaggedClass work

This commit is contained in:
Julien Valverdé
2024-06-13 00:47:56 +02:00
parent 1daa37faf4
commit 2a9217e2a1
4 changed files with 50 additions and 3 deletions

View File

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

View File

@@ -0,0 +1,20 @@
import { Schema as S } from "@effect/schema"
import type { Annotations, PropertySignature, Struct } from "@effect/schema/Schema"
import type { HasFields } from "./util"
export function TaggedClass<Self>(identifier?: string) {
return <
Tag extends string,
Fields extends Struct.Fields,
>(
tag: Tag,
fieldsOr: Fields | HasFields<Fields>,
annotations?: Annotations.Schema<Self>,
) =>
S.TaggedClass<Self>(identifier)(tag, fieldsOr, annotations) as S.TaggedClass<
Self,
Tag,
{ readonly _tag: PropertySignature<":", Tag, never, ":", Tag, true, never> } & Fields
>
}

View File

@@ -3,3 +3,4 @@ export { MobXObservable } from "./MobXObservable"
export { Mutable } from "./Mutable"
export { MutableClass } from "./MutableClass"
export type { TMutableClass } from "./TMutableClass"
export { TaggedClass } from "./TaggedClass"

View File

@@ -4,15 +4,20 @@ import { pipe } from "remeda"
import { Mutable } from "./Mutable"
import { MobXObservable } from "./MobXObservable"
import { Class } from "./Class"
import { TaggedClass } from "./TaggedClass"
const UserSuper = <Self>() => pipe(
Class<Self>("User")({
// Class<Self>("User")({
// id: S.BigIntFromSelf,
// role: S.Union(S.Literal("BasicUser"), S.Literal("Admin")),
// }),
TaggedClass<Self>()("User", {
id: S.BigIntFromSelf,
role: S.Union(S.Literal("BasicUser"), S.Literal("Admin")),
}),
Mutable,
// Mutable,
MobXObservable,
)
@@ -24,7 +29,7 @@ user1.id = 1n
class Admin extends User.extend<Admin>("Admin")({
role: S.Literal("Admin")
// role: S.Literal("Admin")
}) {}
const user2 = new Admin({ id: 2n, role: "Admin" })