TaggedClass work
This commit is contained in:
21
src/effect/schema/class/MutableTaggedClass.ts
Normal file
21
src/effect/schema/class/MutableTaggedClass.ts
Normal 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>,
|
||||||
|
{},
|
||||||
|
{}
|
||||||
|
>
|
||||||
|
}
|
||||||
20
src/effect/schema/class/TaggedClass.ts
Normal file
20
src/effect/schema/class/TaggedClass.ts
Normal 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
|
||||||
|
>
|
||||||
|
}
|
||||||
@@ -3,3 +3,4 @@ export { MobXObservable } from "./MobXObservable"
|
|||||||
export { Mutable } from "./Mutable"
|
export { Mutable } from "./Mutable"
|
||||||
export { MutableClass } from "./MutableClass"
|
export { MutableClass } from "./MutableClass"
|
||||||
export type { TMutableClass } from "./TMutableClass"
|
export type { TMutableClass } from "./TMutableClass"
|
||||||
|
export { TaggedClass } from "./TaggedClass"
|
||||||
|
|||||||
@@ -4,15 +4,20 @@ import { pipe } from "remeda"
|
|||||||
import { Mutable } from "./Mutable"
|
import { Mutable } from "./Mutable"
|
||||||
import { MobXObservable } from "./MobXObservable"
|
import { MobXObservable } from "./MobXObservable"
|
||||||
import { Class } from "./Class"
|
import { Class } from "./Class"
|
||||||
|
import { TaggedClass } from "./TaggedClass"
|
||||||
|
|
||||||
|
|
||||||
const UserSuper = <Self>() => pipe(
|
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,
|
id: S.BigIntFromSelf,
|
||||||
role: S.Union(S.Literal("BasicUser"), S.Literal("Admin")),
|
role: S.Union(S.Literal("BasicUser"), S.Literal("Admin")),
|
||||||
}),
|
}),
|
||||||
|
|
||||||
Mutable,
|
// Mutable,
|
||||||
MobXObservable,
|
MobXObservable,
|
||||||
)
|
)
|
||||||
|
|
||||||
@@ -24,7 +29,7 @@ user1.id = 1n
|
|||||||
|
|
||||||
|
|
||||||
class Admin extends User.extend<Admin>("Admin")({
|
class Admin extends User.extend<Admin>("Admin")({
|
||||||
role: S.Literal("Admin")
|
// role: S.Literal("Admin")
|
||||||
}) {}
|
}) {}
|
||||||
|
|
||||||
const user2 = new Admin({ id: 2n, role: "Admin" })
|
const user2 = new Admin({ id: 2n, role: "Admin" })
|
||||||
|
|||||||
Reference in New Issue
Block a user