84 lines
2.4 KiB
TypeScript
84 lines
2.4 KiB
TypeScript
import { Schema as S } from "@effect/schema"
|
|
import { AbstractConstructor, Simplify } from "type-fest"
|
|
import { Trait, TraitTuple } from "../Trait"
|
|
import { StaticImplements, TraitExpressionLike } from "../TraitExpression"
|
|
import { Extend, StaticMembers } from "../util"
|
|
|
|
|
|
export class EffectSchemaTraitExpression<
|
|
Fields extends S.Struct.Fields,
|
|
I, R, C,
|
|
Inherited extends object,
|
|
Proto,
|
|
Static extends object,
|
|
|
|
const Traits extends readonly Trait<any, any, any, any>[],
|
|
|
|
const Mutability extends "Immutable" | "Mutable",
|
|
const EncodedMutability extends "Immutable" | "Mutable",
|
|
>
|
|
implements TraitExpressionLike<
|
|
S.Class<unknown, Fields, I, R, C, Inherited, Proto>,
|
|
Traits
|
|
> {
|
|
constructor(
|
|
readonly superclass: S.Class<unknown, Fields, I, R, C, Inherited, Proto>,
|
|
readonly superclassStatic: Static,
|
|
readonly traits: Traits,
|
|
readonly mutability: Mutability,
|
|
readonly encodedMutability: EncodedMutability,
|
|
) {}
|
|
|
|
|
|
extends<Self>(): (
|
|
AbstractConstructor<
|
|
Simplify<
|
|
ApplyMutability<S.Struct.Type<Fields>, Mutability> &
|
|
Extend<[
|
|
Omit<Inherited, keyof Fields>,
|
|
...TraitTuple.MapImplInstance<Traits>
|
|
]> &
|
|
Proto
|
|
>,
|
|
|
|
ConstructorParameters<S.Class<unknown, Fields, I, R, C, Inherited, Proto>>
|
|
> &
|
|
|
|
StaticMembers<
|
|
S.Class<
|
|
Self,
|
|
Fields,
|
|
ApplyMutability<I, EncodedMutability>,
|
|
R, C,
|
|
Inherited,
|
|
Proto
|
|
>
|
|
> &
|
|
Simplify<
|
|
Extend<[
|
|
Static,
|
|
...TraitTuple.MapImplStaticMembers<Traits>
|
|
]>
|
|
>
|
|
) {
|
|
return this.traits.reduce(
|
|
(previous, trait) => trait.apply(previous),
|
|
this.superclass,
|
|
) as any
|
|
}
|
|
|
|
|
|
staticImplements(_target: StaticImplements<typeof this>, _context: any) {}
|
|
|
|
get staticImplementsStage2() {
|
|
return (_target: StaticImplements<typeof this>) => {}
|
|
}
|
|
}
|
|
|
|
|
|
type ApplyMutability<T, Mutability extends "Immutable" | "Mutable"> = (
|
|
Mutability extends "Immutable"
|
|
? { +readonly [P in keyof T]: T[P] }
|
|
: { -readonly [P in keyof T]: T[P] }
|
|
)
|