From 9f2c42db1432631527f68e3b0336dbf5f0d011ab Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Julien=20Valverd=C3=A9?= Date: Fri, 16 Feb 2024 01:14:36 +0100 Subject: [PATCH] Refactoring --- src/Trait.ts | 89 ++++++++++++++++++------------------------ src/TraitExpression.ts | 35 +++++++++++------ 2 files changed, 63 insertions(+), 61 deletions(-) diff --git a/src/Trait.ts b/src/Trait.ts index 91df43a..3f2e39f 100644 --- a/src/Trait.ts +++ b/src/Trait.ts @@ -1,23 +1,7 @@ import { Fn, Pipe, Tuples } from "hotscript" import { AbstractClass, Class } from "type-fest" import { TraitExpression } from "./TraitExpression" -import { ExtendFn, SimplifyFn, StaticMembers, StaticMembersFn } from "./util" - - -type AddAbstractToImplClass< - ImplClass extends Class, - Abstract extends object, - StaticAbstract extends object, -> = ( - Class< - Abstract & - InstanceType, - - ConstructorParameters - > & - StaticAbstract & - StaticMembers -) +import { Extend, ExtendFn, SimplifyFn, StaticMembers, StaticMembersFn } from "./util" export class Trait< @@ -82,14 +66,18 @@ export namespace Trait { } export type OwnClass = ( - AddAbstractToImplClass, Trait.OwnAbstract, Trait.OwnStaticAbstract> + AbstractClass, any[]> & + Extend<[ + Trait.OwnStaticAbstract, + StaticMembers>, + ]> ) export interface OwnClassFn extends Fn { return: Trait.OwnClass } export type OwnInstance = ( - InstanceType> + Extend<[Trait.OwnAbstract, Trait.OwnImplInstance]> ) export interface OwnInstanceFn extends Fn { return: Trait.OwnInstance @@ -101,35 +89,36 @@ export namespace Trait { export interface SupertraitsFn extends Fn { return: Trait.Supertraits } - - export type Class = ( - AbstractClass< - Trait.Instance, - any[] - > & - Pipe, - Tuples.Map, - Tuples.Map, - ExtendFn, - SimplifyFn, - ]> - ) - export interface ClassFn extends Fn { - return: Trait.Class - } - - export type Instance = ( - Pipe, - Tuples.Map, - ExtendFn, - SimplifyFn, - ]> - ) - export interface InstanceFn extends Fn { - return: Trait.Instance - } } + + +export type TraitClass> = ( + AbstractClass, any[]> & + TraitStaticMembers +) + +export type TraitConcreteClass> = ( + Class, any[]> & + TraitStaticMembers +) + +export type TraitInstance> = ( + Pipe, + Tuples.Map, + ExtendFn, + SimplifyFn, + ]> +) + +export type TraitStaticMembers> = ( + Pipe, + Tuples.Map, + Tuples.Map, + ExtendFn, + SimplifyFn, + ]> +) diff --git a/src/TraitExpression.ts b/src/TraitExpression.ts index 73c467b..b631dbc 100644 --- a/src/TraitExpression.ts +++ b/src/TraitExpression.ts @@ -1,5 +1,5 @@ import { Fn, Pipe, Tuples } from "hotscript" -import { AbstractClass } from "type-fest" +import { AbstractClass, Class } from "type-fest" import { Trait } from "./Trait" import { TraitBuilder } from "./TraitBuilder" import { ExtendFn, SimplifyFn, StaticMembersFn } from "./util" @@ -59,6 +59,7 @@ export class TraitExpression< export namespace TraitExpression { export class NullSuperclass { static readonly _tag = "@thilawyn/traitify-ts/TraitExpression.NullSuperclass" + constructor(...args: any[]) {} } export type Superclass = ( @@ -102,25 +103,37 @@ export type ImplementsStatic> = ( export type TraitExpressionClass> = ( AbstractClass< + TraitExpressionInstance, + ConstructorParameters> > & - - + TraitExpressionStaticMembers ) export type TraitExpressionConcreteClass> = ( - + Class< + TraitExpressionInstance, + ConstructorParameters> + > & + TraitExpressionStaticMembers ) export type TraitExpressionInstance> = ( - + Pipe, [ + Tuples.Map, // Map all the traits to their instance representation + Tuples.Prepend< // Add the instance of the superclass at the top of the list + InstanceType> + >, + ExtendFn, // Reduce to a single object that extends all the objects in the list + SimplifyFn, // Make readable for IDEs + ]> ) export type TraitExpressionStaticMembers> = ( - Pipe, // Map all the traits to their implementation class - Tuples.Prepend, // Add the superclass at the top of the list - Tuples.Map, // Map all the classes to an object containing their static members - ExtendFn, // Reduce to a single object that extends all the objects in the list - SimplifyFn, // Make readable for IDEs + Pipe, [ + Tuples.Map, // Map all the traits to their class representation + Tuples.Prepend>, // Add the superclass at the top of the list + Tuples.Map, // Map all the classes to an object containing their static members + ExtendFn, // Reduce to a single object that extends all the objects in the list + SimplifyFn, // Make readable for IDEs ]> )