From e2d53644870840ef9662a27b3d7634ac8a3a8882 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Julien=20Valverd=C3=A9?= Date: Sat, 24 Feb 2024 21:06:44 +0100 Subject: [PATCH] TraitBuilder refactoring --- src/TraitBuilder.ts | 73 +++++++++++++++++++++++++-------------------- 1 file changed, 40 insertions(+), 33 deletions(-) diff --git a/src/TraitBuilder.ts b/src/TraitBuilder.ts index b6f698a..b0e4f3c 100644 --- a/src/TraitBuilder.ts +++ b/src/TraitBuilder.ts @@ -6,34 +6,6 @@ import { Extend, StaticMembers } from "./util" declare const implSuperSymbol: unique symbol -type ImplSuper = ( - This extends TraitBuilder< - any, - infer Abstract, - infer StaticAbstract, - infer ImplClass - > - ? ( - AbstractClass< - Simplify< - Extend<[ - Abstract, - InstanceType, - ]> - > - > & - - Simplify< - Extend<[ - StaticAbstract, - StaticMembers, - ]> - > & - { readonly [implSuperSymbol]: true } - ) - : never -) - export class TraitBuilder< SuperExpression extends TraitExpression< @@ -44,6 +16,8 @@ export class TraitBuilder< StaticAbstract extends object, ImplClass extends AbstractClass, > { + declare ["constructor"]: typeof TraitBuilder + constructor( readonly traitSuperExpression: SuperExpression, readonly traitAbstract: Abstract, @@ -51,10 +25,11 @@ export class TraitBuilder< readonly traitApply: (Super: AbstractClass) => ImplClass, ) {} + abstract( _: (Super: AbstractClass) => AbstractClass ) { - return new TraitBuilder( + return new this.constructor( this.traitSuperExpression, {} as Simplify, this.traitStaticAbstract, @@ -65,7 +40,7 @@ export class TraitBuilder< staticAbstract( _: (Super: AbstractClass) => AbstractClass ) { - return new TraitBuilder( + return new this.constructor( this.traitSuperExpression, this.traitAbstract, {} as Simplify, @@ -74,11 +49,11 @@ export class TraitBuilder< } implement< - ImplClassWithAbstract extends ImplSuper // TODO: find a way to set the constraint to concrete classes while keeping the Super arg as an abstract class + ImplClassWithAbstract extends TraitBuilder.ImplSuper // TODO: find a way to set the constraint to concrete classes while keeping the Super arg as an abstract class >( - apply: (Super: ImplSuper) => ImplClassWithAbstract + apply: (Super: TraitBuilder.ImplSuper) => 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 extends TraitBuilder< + any, + infer Abstract, + infer StaticAbstract, + infer ImplClass + > + ? ( + AbstractClass< + Simplify< + Extend<[ + Abstract, + InstanceType, + ]> + > + > & + + Simplify< + Extend<[ + StaticAbstract, + StaticMembers, + ]> + > & + { readonly [implSuperSymbol]: true } + ) + : never + ) +} + + export const trait = new TraitBuilder( new TraitExpression(TraitExpression.NullSuperclass, []), {} as object,