diff --git a/src/TraitExpression.ts b/src/TraitExpression.ts index 3c3a5fb..e789b05 100644 --- a/src/TraitExpression.ts +++ b/src/TraitExpression.ts @@ -1,8 +1,8 @@ import { Fn, Pipe, Tuples } from "hotscript" import { AbstractClass, Class, Simplify } from "type-fest" -import { Trait, TraitInstance, Traits } from "./Trait" +import { Trait, TraitInstance, TraitStaticMembers, Traits } from "./Trait" import { TraitBuilder } from "./TraitBuilder" -import { ExtendFn, ExtendPlain, SimplifyFn, StaticMembersFn } from "./util" +import { ExtendFn, ExtendPlain, SimplifyFn, StaticMembers, StaticMembersFn } from "./util" export class TraitExpression< @@ -148,37 +148,35 @@ export type TraitExpressionConcreteClass< export type TraitExpressionInstance< Exp extends TraitExpression[]> > = ( - // 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 - // ]> Simplify< ExtendPlain<[ InstanceType>, - ...MapTraitsInstance< - TraitExpression.Traits - >, + ...MapTraitsInstance>, ]> > ) -type MapTraitsInstance[]> = { +type MapTraitsInstance = { [K in keyof T]: K extends keyof [] ? T[K] - : TraitInstance + : T[K] extends Trait + ? TraitInstance + : never } export type TraitExpressionStaticMembers< Exp extends TraitExpression[]> > = ( - 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 - ]> + Simplify< + ExtendPlain<[ + StaticMembers>, + ...MapTraitsStaticMembers>, + ]> + > ) +type MapTraitsStaticMembers = { + [K in keyof T]: K extends keyof [] + ? T[K] + : T[K] extends Trait + ? TraitStaticMembers + : never +} diff --git a/src/tests.ts b/src/tests.ts index bc0ad49..631b011 100644 --- a/src/tests.ts +++ b/src/tests.ts @@ -1,6 +1,6 @@ import { TraitClass } from "./Trait" import { trait } from "./TraitBuilder" -import { Implements, ImplementsStatic } from "./TraitExpression" +import { Implements, ImplementsStatic, TraitExpressionClass } from "./TraitExpression" import { expression } from "./TraitExpressionBuilder" @@ -70,6 +70,7 @@ const exp = expression type Abs = Implements type AbsStatic = ImplementsStatic +type ExpClass = TraitExpressionClass @exp.implementsStatic class User extends exp.extends implements Implements {