Co-authored-by: Julien Valverdé <julien.valverde@mailo.com> Reviewed-on: https://git.jvalver.de/Thilawyn/traitify-ts/pulls/21
This commit was merged in pull request #21.
This commit is contained in:
84
src/effect/EffectSchemaTraitExpression.ts
Normal file
84
src/effect/EffectSchemaTraitExpression.ts
Normal file
@@ -0,0 +1,84 @@
|
||||
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<
|
||||
ApplyMutability<S.Struct.Type<Fields>, Mutability> &
|
||||
Omit<Inherited, keyof Fields> &
|
||||
Proto,
|
||||
|
||||
ConstructorParameters<S.Class<unknown, Fields, I, R, C, Inherited, Proto>>
|
||||
> &
|
||||
|
||||
StaticMembers<
|
||||
S.Class<
|
||||
Self,
|
||||
Fields,
|
||||
ApplyMutability<I, EncodedMutability>,
|
||||
R, C,
|
||||
Simplify<
|
||||
Extend<[
|
||||
Inherited,
|
||||
...TraitTuple.MapImplInstance<Traits>
|
||||
]>
|
||||
>,
|
||||
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] }
|
||||
)
|
||||
Reference in New Issue
Block a user