Fixed TraitExpression
All checks were successful
continuous-integration/drone/push Build is passing

This commit is contained in:
Julien Valverdé
2024-02-19 17:07:28 +01:00
parent 4ba0a19037
commit ec82537380

View File

@@ -1,8 +1,7 @@
import { Fn, Pipe, Tuples } from "hotscript"
import { AbstractClass, Class, Simplify } from "type-fest"
import { Trait, TraitInstance, TraitStaticMembers, Traits } from "./Trait"
import { TraitBuilder } from "./TraitBuilder"
import { ExtendFn, ExtendPlain, SimplifyFn, StaticMembers, StaticMembersFn } from "./util"
import { ExtendPlain, StaticMembers } from "./util"
export class TraitExpression<
@@ -16,23 +15,22 @@ export class TraitExpression<
get extends(): (
AbstractClass<
Pipe<T, [
Tuples.Map<Trait.OwnImplInstanceFn>, // Map all the traits to the instance of their implementation class
Tuples.Prepend<InstanceType<Superclass>>, // Add the instance of the superclass at the top of the list
ExtendFn, // Reduce to a single instance that extends all the instances in the list
SimplifyFn, // Make readable for IDEs
]>,
Simplify<
ExtendPlain<[
InstanceType<Superclass>,
...Traits.MapImplInstance<T>,
]>
>,
ConstructorParameters<Superclass>
> &
Pipe<T, [
Tuples.Map<Trait.OwnImplClassFn>, // Map all the traits to their implementation class
Tuples.Prepend<Superclass>, // Add the superclass at the top of the list
Tuples.Map<StaticMembersFn>, // 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<Superclass>,
...Traits.MapImplStaticMembers<T>,
]>
>
) {
return this.traits.reduce(
(previous, trait) => trait.apply(previous),
@@ -85,18 +83,12 @@ export namespace TraitExpression {
? Superclass
: never
)
export interface SuperclassFn extends Fn {
return: TraitExpression.Superclass<this["arg0"]>
}
export type Traits<T> = (
T extends TraitExpression<any, infer Traits>
? Traits
: never
)
export interface TraitsFn extends Fn {
return: TraitExpression.Traits<this["arg0"]>
}
}