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

This commit is contained in:
Julien Valverdé
2024-02-24 21:06:44 +01:00
parent 2e3878e08d
commit e2d5364487

View File

@@ -6,34 +6,6 @@ import { Extend, StaticMembers } from "./util"
declare const implSuperSymbol: unique symbol
type ImplSuper<This> = (
This extends TraitBuilder<
any,
infer Abstract,
infer StaticAbstract,
infer ImplClass
>
? (
AbstractClass<
Simplify<
Extend<[
Abstract,
InstanceType<ImplClass>,
]>
>
> &
Simplify<
Extend<[
StaticAbstract,
StaticMembers<ImplClass>,
]>
> &
{ readonly [implSuperSymbol]: true }
)
: never
)
export class TraitBuilder<
SuperExpression extends TraitExpression<
@@ -44,6 +16,8 @@ export class TraitBuilder<
StaticAbstract extends object,
ImplClass extends AbstractClass<object, []>,
> {
declare ["constructor"]: typeof TraitBuilder
constructor(
readonly traitSuperExpression: SuperExpression,
readonly traitAbstract: Abstract,
@@ -51,10 +25,11 @@ export class TraitBuilder<
readonly traitApply: (Super: AbstractClass<object>) => ImplClass,
) {}
abstract<A extends Abstract>(
_: (Super: AbstractClass<Abstract>) => AbstractClass<A, []>
) {
return new TraitBuilder(
return new this.constructor(
this.traitSuperExpression,
{} as Simplify<A>,
this.traitStaticAbstract,
@@ -65,7 +40,7 @@ export class TraitBuilder<
staticAbstract<A extends StaticAbstract>(
_: (Super: AbstractClass<StaticAbstract>) => AbstractClass<A, []>
) {
return new TraitBuilder(
return new this.constructor(
this.traitSuperExpression,
this.traitAbstract,
{} as Simplify<A>,
@@ -74,11 +49,11 @@ export class TraitBuilder<
}
implement<
ImplClassWithAbstract extends ImplSuper<typeof this> // TODO: find a way to set the constraint to concrete classes while keeping the Super arg as an abstract class
ImplClassWithAbstract extends TraitBuilder.ImplSuper<typeof this> // TODO: find a way to set the constraint to concrete classes while keeping the Super arg as an abstract class
>(
apply: (Super: ImplSuper<typeof this>) => ImplClassWithAbstract
apply: (Super: TraitBuilder.ImplSuper<typeof this>) => ImplClassWithAbstract
) {
return new TraitBuilder(
return new this.constructor(
this.traitSuperExpression,
this.traitAbstract,
this.traitStaticAbstract,
@@ -103,6 +78,7 @@ export class TraitBuilder<
)
}
build() {
return new Trait(
this.traitSuperExpression,
@@ -113,6 +89,37 @@ export class TraitBuilder<
}
}
export namespace TraitBuilder {
export type ImplSuper<This> = (
This extends TraitBuilder<
any,
infer Abstract,
infer StaticAbstract,
infer ImplClass
>
? (
AbstractClass<
Simplify<
Extend<[
Abstract,
InstanceType<ImplClass>,
]>
>
> &
Simplify<
Extend<[
StaticAbstract,
StaticMembers<ImplClass>,
]>
> &
{ readonly [implSuperSymbol]: true }
)
: never
)
}
export const trait = new TraitBuilder(
new TraitExpression(TraitExpression.NullSuperclass, []),
{} as object,