Files
thilalib/src/effect/schema/MutableClass.ts
Julien Valverdé cc33865e1a MutableClass work
2024-06-10 21:03:23 +02:00

66 lines
2.0 KiB
TypeScript

import { Schema as S } from "@effect/schema"
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, MissingSelfGeneric } 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 = never>(identifier: string): <newFields extends Struct.Fields>(
fields: newFields | HasFields<newFields>,
annotations?: Annotations.Schema<Extended>,
) => [Extended] extends [never]
? MissingSelfGeneric<"Base.extend">
: TMutableClass<
Extended,
Fields & newFields,
I & Struct.Encoded<newFields>,
R | Struct.Context<newFields>,
C & Struct.Constructor<newFields>,
Self,
Proto
>
}
)
export const MutableClass = <Self = never>(identifier: string) => (
<Fields extends Struct.Fields>(
fieldsOr: Fields | HasFields<Fields>,
annotations?: Annotations.Schema<Self>,
): (
[Self] extends [never]
? MissingSelfGeneric<"Class">
: TMutableClass<
Self,
Fields,
Struct.Encoded<Fields>,
Struct.Context<Fields>,
Struct.Constructor<Fields>,
{},
{}
>
) =>
S.Class<Self>(identifier)(fieldsOr, annotations)
)