MutableTaggedClass

This commit is contained in:
Julien Valverdé
2024-06-14 21:26:26 +02:00
parent dc5c56ddef
commit 27c80a7b1e
5 changed files with 16 additions and 106 deletions

View File

@@ -1,37 +0,0 @@
import type { Class, Struct } from "@effect/schema/Schema"
import type { Constructor } from "type-fest"
import type { StaticType } from "../../.."
import type { TMutableClass } from "./TMutableClass"
export function Mutable<
This extends Class<Self, Fields, I, R, C, Inherited, Proto>,
Self,
Fields extends Struct.Fields,
I, R, C,
Inherited,
Proto,
>(
class_:
| This
| Class<Self, Fields, I, R, C, Inherited, Proto>
) {
return class_ as (
Constructor<
Omit<
InstanceType<This>,
keyof InstanceType<Class<Self, Fields, I, R, C, Inherited, Proto>>
> &
InstanceType<TMutableClass<Self, Fields, I, R, C, Inherited, Proto>>,
ConstructorParameters<TMutableClass<Self, Fields, I, R, C, Inherited, Proto>>
> &
Omit<
StaticType<This>,
keyof StaticType<Class<Self, Fields, I, R, C, Inherited, Proto>>
> &
StaticType<TMutableClass<Self, Fields, I, R, C, Inherited, Proto>>
)
}

View File

@@ -1,5 +1,6 @@
import { Schema as S } from "@effect/schema"
import type { Annotations, PropertySignature, Struct } from "@effect/schema/Schema"
import type { Annotations, Struct } from "@effect/schema/Schema"
import type { IMutableClass } from "./MutableClass"
import type { HasFields } from "./util"
@@ -12,14 +13,14 @@ export function MutableTaggedClass<Self>(identifier?: string) {
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
// >
S.TaggedClass<Self>(identifier)(tag, fieldsOr, annotations) as S.TaggedClass<
S.TaggedClass<Self>(identifier)(tag, fieldsOr, annotations) as IMutableClass<
Self,
Tag,
{ readonly _tag: PropertySignature<":", Tag, never, ":", Tag, true, never> } & Fields
>
Fields,
Struct.Encoded<Fields>,
Struct.Context<Fields>,
Struct.Constructor<Omit<Fields, "_tag">>,
{},
{}
> &
{ readonly _tag: Tag }
}

View File

@@ -1,42 +0,0 @@
import type { Annotations, Class, Struct } from "@effect/schema/Schema"
import type { Mutable } from "effect/Types"
import type { Constructor } from "type-fest"
import type { StaticType } from "../../.."
import type { HasFields } from "./util"
export type TMutableClass<
Self,
Fields extends Struct.Fields,
I, R, C,
Inherited,
Proto,
> = (
Constructor<
Omit<
InstanceType<Class<Self, Fields, I, R, C, Inherited, Proto>>,
keyof Fields
> &
Mutable<Struct.Type<Fields>>,
ConstructorParameters<Class<Self, Fields, I, R, C, Inherited, Proto>>
> &
Omit<
StaticType<Class<Self, Fields, I, R, C, Inherited, Proto>>,
"extend"
> & {
extend<Extended>(identifier: string): <newFields extends Struct.Fields>(
fields: newFields | HasFields<newFields>,
annotations?: Annotations.Schema<Extended>,
) => TMutableClass<
Extended,
Fields & newFields,
I & Struct.Encoded<newFields>,
R | Struct.Context<newFields>,
C & Struct.Constructor<newFields>,
Self,
Proto
>
}
)

View File

@@ -1,6 +1,6 @@
export { Class } from "./Class"
export { MobXObservable } from "./MobXObservable"
export { Mutable } from "./Mutable"
export { MutableClass } from "./MutableClass"
export type { IMutableClass } from "./MutableClass"
export { MutableTaggedClass } from "./MutableTaggedClass"
export { TaggedClass } from "./TaggedClass"

View File

@@ -1,28 +1,16 @@
import { Schema as S } from "@effect/schema"
import { MutableClass } from "./MutableClass"
import { pipe } from "remeda"
import { Mutable } from "./Mutable"
import { MobXObservable } from "./MobXObservable"
import { Class } from "./Class"
import { TaggedClass } from "./TaggedClass"
import { MutableTaggedClass } from "./MutableTaggedClass"
const UserSuper = <Self>() => pipe(
MutableClass<Self>("User")({
class User extends pipe(
MutableTaggedClass<User>()("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,
MobXObservable,
)
class User extends UserSuper<User>() {}
) {}
const user1 = new User({ id: 1n, role: "BasicUser" })
user1.id = 1n