This commit is contained in:
Julien Valverdé
2024-06-11 16:54:46 +02:00
parent 98a48a8982
commit fe5a863d9b
2 changed files with 23 additions and 14 deletions

View File

@@ -1,17 +1,15 @@
import { Schema as S } from "@effect/schema" import { Schema as S } from "@effect/schema"
import type { Annotations, Struct } from "@effect/schema/Schema" import type { Annotations, Struct } from "@effect/schema/Schema"
import type { TMutableClass } from "./TMutableClass" import type { TMutableClass } from "./TMutableClass"
import type { HasFields, MissingSelfGeneric } from "./util" import type { HasFields } from "./util"
export function MutableClass<Self = never>(identifier: string) { export function MutableClass<Self = never>(identifier: string) {
return <Fields extends Struct.Fields>( return <Fields extends Struct.Fields>(
fieldsOr: Fields | HasFields<Fields>, fieldsOr: Fields | HasFields<Fields>,
annotations?: Annotations.Schema<Self>, annotations?: Annotations.Schema<Self>,
): ( ) =>
[Self] extends [never] S.Class<Self>(identifier)(fieldsOr, annotations) as TMutableClass<
? MissingSelfGeneric<"Class">
: TMutableClass<
Self, Self,
Fields, Fields,
Struct.Encoded<Fields>, Struct.Encoded<Fields>,
@@ -20,6 +18,4 @@ export function MutableClass<Self = never>(identifier: string) {
{}, {},
{} {}
> >
) =>
S.Class<Self>(identifier)(fieldsOr, annotations)
} }

View File

@@ -1,5 +1,18 @@
import { Schema as S } from "@effect/schema" import { Schema as S } from "@effect/schema"
import { MutableClass } from "./MutableClass" import { MutableClass } from "./MutableClass"
import { pipe } from "remeda"
import { Mutable } from "./Mutable"
import { MobXObservable } from "./MobXObservable"
const UserSuper = <Self>() => pipe(
MutableClass<Self>("User")({
id: S.BigIntFromSelf,
role: S.Union(S.Literal("BasicUser"), S.Literal("Admin")),
}),
v => MobXObservable(v),
)
class User extends MutableClass<User>("User")({ class User extends MutableClass<User>("User")({