TraitBuilder
All checks were successful
continuous-integration/drone/push Build is passing

This commit is contained in:
Julien Valverdé
2024-02-14 01:37:05 +01:00
parent cb0dd26ccb
commit a9181567a2
3 changed files with 91 additions and 12 deletions

View File

@@ -5,7 +5,7 @@ import { AbstractTag } from "./abstract"
import { ExtendFn, SimplifyFn, StaticMembers, StaticMembersFn } from "./util" import { ExtendFn, SimplifyFn, StaticMembers, StaticMembersFn } from "./util"
export type TraitApplierSuperTag = "@thilawyn/traitify-ts/TraitApplierSuper" export type TraitApplierSuperTag = "@thilawyn/traitify-ts/Super"
export type AddAbstractToImplClass< export type AddAbstractToImplClass<

View File

@@ -1,21 +1,100 @@
import { Class } from "type-fest" import { AbstractClass, Class } from "type-fest"
import { Trait } from "./Trait" import { Trait } from "./Trait"
import { TraitExpression } from "./TraitExpression" import { TraitExpression } from "./TraitExpression"
import { StaticMembers } from "./util"
/* export type TraitApplierSuperTag = "@thilawyn/traitify-ts/Super"
Ideas:
- Make .abstract() and .staticAbstract() merge the declaration with the current Abstract or StaticAbstract export type RemoveAbstractFromImplClass<
*/ ImplClassWithAbstract extends (
class TraitBuilder< Class<Abstract, []> &
Supertraits extends TraitExpression< StaticAbstract &
{ _tag: TraitApplierSuperTag }
),
Abstract extends object,
StaticAbstract extends object,
> = (
Class<
Omit<InstanceType<ImplClassWithAbstract>, keyof Abstract>,
ConstructorParameters<ImplClassWithAbstract>
> &
Omit<StaticMembers<ImplClassWithAbstract>, keyof StaticAbstract | "_tag">
)
export class TraitBuilder<
SuperExpression extends TraitExpression<
typeof TraitExpression.NullSuperclass, typeof TraitExpression.NullSuperclass,
Trait<any, any, any, any>[], Trait<any, any, any, any>[],
Trait<any, any, any, any>[] Trait<any, any, any, any>[]
>, >,
Abstract extends {}, Abstract extends object,
StaticAbstract extends {}, StaticAbstract extends object,
ImplClass extends Class<{}, []>, ImplClass extends Class<object, []>,
> { > {
constructor(
private readonly traitSuperExpression: SuperExpression,
private readonly traitAbstract: Abstract,
private readonly traitStaticAbstract: StaticAbstract,
private readonly traitApply: (Super: AbstractClass<object>) => ImplClass,
) {}
abstract<A extends Abstract>() {
return new TraitBuilder(
this.traitSuperExpression,
{} as A,
this.traitStaticAbstract,
this.traitApply,
)
} }
staticAbstract<A extends StaticAbstract>() {
return new TraitBuilder(
this.traitSuperExpression,
this.traitAbstract,
{} as A,
this.traitApply,
)
}
implement<
ImplClassWithAbstract extends (
Class<Abstract, []> &
StaticAbstract &
{ _tag: TraitApplierSuperTag }
)
>(
apply: (
Super: (
AbstractClass<Abstract> &
StaticAbstract &
{ _tag: TraitApplierSuperTag }
)
) => ImplClassWithAbstract
) {
return new Trait(
this.traitSuperExpression,
this.traitAbstract,
this.traitStaticAbstract,
apply as unknown as (Super: AbstractClass<object>) => RemoveAbstractFromImplClass<ImplClassWithAbstract, Abstract, StaticAbstract>,
)
}
build() {
return new Trait(
this.traitSuperExpression,
this.traitAbstract,
this.traitStaticAbstract,
this.traitApply,
)
}
}
export const trait = new TraitBuilder(
new TraitExpression(TraitExpression.NullSuperclass, [], []),
{},
{},
Super => class extends Super {},
)

View File

@@ -69,7 +69,7 @@ class TraitExpressionBuilder<
const OwnTraits extends Trait<any, any, any, any>[], const OwnTraits extends Trait<any, any, any, any>[],
const AllTraits extends Trait<any, any, any, any>[], const AllTraits extends Trait<any, any, any, any>[],
> { > {
constructor(private expression: TraitExpression<Superclass, OwnTraits, AllTraits>) {} constructor(private readonly expression: TraitExpression<Superclass, OwnTraits, AllTraits>) {}
extends< extends<
Super extends AbstractClass<any> Super extends AbstractClass<any>