From e5e8b7fad751b0920c99e555670356272868c770 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Julien=20Valverd=C3=A9?= Date: Sat, 17 Feb 2024 04:11:17 +0100 Subject: [PATCH] Pure TS implementation attempt --- src/TraitBuilder.ts | 28 ++++++++++++++++++++-------- src/tests.ts | 28 +++++++++++++++++++++++++--- src/util/extend.ts | 18 ++++++++++++++++-- 3 files changed, 61 insertions(+), 13 deletions(-) diff --git a/src/TraitBuilder.ts b/src/TraitBuilder.ts index 3663194..a576ecd 100644 --- a/src/TraitBuilder.ts +++ b/src/TraitBuilder.ts @@ -2,10 +2,16 @@ import { Pipe, Tuples } from "hotscript" import { AbstractClass, Class } from "type-fest" import { Trait, TraitClass, TraitConcreteClass } from "./Trait" import { TraitExpression } from "./TraitExpression" -import { ExtendFn, SimplifyFn, StaticMembers } from "./util" +import { ExtendFn, ExtendPlain, SimplifyFn, StaticMembers } from "./util" -type ExtendAbstractSuper = ( +export type ExtendAbstractSuper< + SuperExpression extends TraitExpression< + typeof TraitExpression.NullSuperclass, + Trait[] + >, + Abstract extends object, +> = ( Pipe, @@ -13,6 +19,12 @@ type ExtendAbstractSuper = ( ExtendFn, SimplifyFn, ]> + // ExtendPlain<[ + // ...Trait.OwnAbstract< + // TraitExpression.Traits[number] + // >, + // Abstract, + // ]> ) type ExtendStaticAbstractSuper = ( @@ -30,19 +42,20 @@ type TraitApplierSuperTag = "@thilawyn/traitify-ts/Super" type RemoveAbstractFromImplClass< ImplClassWithAbstract extends AbstractClass, - Abstract extends AbstractClass, + Abstract extends object, + StaticAbstract extends object, > = ( Class< Omit< InstanceType, - keyof InstanceType + keyof Abstract >, ConstructorParameters > & Omit< StaticMembers, - keyof StaticMembers | "_tag" + keyof StaticAbstract | "_tag" > ) @@ -134,9 +147,8 @@ export class TraitBuilder< this.traitStaticAbstract, apply as unknown as (Super: AbstractClass) => RemoveAbstractFromImplClass< ImplClassWithAbstract, - TraitClass< - Trait - > + ExtendAbstractSuper, + ExtendStaticAbstractSuper >, ) } diff --git a/src/tests.ts b/src/tests.ts index 117a19a..eea1d3a 100644 --- a/src/tests.ts +++ b/src/tests.ts @@ -1,7 +1,25 @@ -import { TraitClass } from "./Trait" +import { Trait, TraitClass } from "./Trait" import { trait } from "./TraitBuilder" -import { Implements, ImplementsStatic } from "./TraitExpression" +import { Implements, ImplementsStatic, TraitExpression } from "./TraitExpression" import { expression } from "./TraitExpressionBuilder" +import { ExtendPlain } from "./util" + + +type ExtendAbstractSuper< + SuperExpression extends TraitExpression< + typeof TraitExpression.NullSuperclass, + Trait[] + >, + Abstract extends object, +> = ( + // ExtendPlain<[ + // ...Trait.OwnAbstract< + // TraitExpression.Traits[number] + // >, + // Abstract, + // ]> + TraitExpression.Traits[number] +) const PrintsHelloOnNew = trait @@ -48,7 +66,7 @@ const StatefulSubscription = trait type StatefulSubscriptionClass = TraitClass const ActiveStatefulSubscription = expression - .expresses(StatefulSubscription) + .expresses(PrintsHelloOnNew, StatefulSubscription) .build() .subtrait() .extendAbstract(Super => class extends Super { @@ -58,6 +76,10 @@ const ActiveStatefulSubscription = expression .build() type ActiveStatefulSubscriptionClass = TraitClass +type Test = ExtendAbstractSuper< + Trait.OwnSuperExpression, + Trait.OwnAbstract +> class TestSuperclass { diff --git a/src/util/extend.ts b/src/util/extend.ts index d675de5..0b162d1 100644 --- a/src/util/extend.ts +++ b/src/util/extend.ts @@ -12,7 +12,21 @@ interface ExtendReducerFn extends Fn { } export type ExtendFn = Tuples.Reduce -export type Extend = Call +export type Extend = Call + + +export type ExtendPlain = ( + T extends [infer Super, infer Self, ...infer Rest] + ? Pick> extends Pick> + ? ExtendPlain<[ + Omit> & Self, + ...Rest, + ]> + : never + : T extends [infer Self] + ? Self + : void +) export type ExtendableFn = ComposeLeft<[ @@ -22,4 +36,4 @@ export type ExtendableFn = ComposeLeft<[ Match.With, ]> ]> -export type Extendable = Call +export type Extendable = Call