This commit is contained in:
@@ -1,45 +1,22 @@
|
||||
import { Fn, Pipe, Tuples } from "hotscript"
|
||||
import { AbstractClass, Class, Simplify } from "type-fest"
|
||||
import { Trait } from "./Trait"
|
||||
import { Trait, Traits } from "./Trait"
|
||||
import { TraitBuilder } from "./TraitBuilder"
|
||||
import { ExtendFn, ExtendPlain, SimplifyFn, StaticMembersFn } from "./util"
|
||||
|
||||
|
||||
type SubtraitAbstract<Exp> = (
|
||||
ExtendPlain<
|
||||
MapTraitsToOwnAbstract<
|
||||
TraitExpression.Traits<Exp>
|
||||
>
|
||||
>
|
||||
)
|
||||
type MapTraitsToOwnAbstract<T extends readonly any[]> = {
|
||||
[K in keyof T]: Trait.OwnAbstract<T[K]>
|
||||
}
|
||||
|
||||
type SubtraitStaticAbstract<Exp> = (
|
||||
ExtendPlain<
|
||||
MapTraitsToOwnStaticAbstract<
|
||||
TraitExpression.Traits<Exp>
|
||||
>
|
||||
>
|
||||
)
|
||||
type MapTraitsToOwnStaticAbstract<T extends readonly any[]> = {
|
||||
[K in keyof T]: Trait.OwnStaticAbstract<T[K]>
|
||||
}
|
||||
|
||||
|
||||
export class TraitExpression<
|
||||
Superclass extends AbstractClass<object>,
|
||||
const Traits extends Trait<any, any, any, any>[],
|
||||
Superclass extends AbstractClass<object>,
|
||||
const T extends Trait<any, any, any, any>[],
|
||||
> {
|
||||
constructor(
|
||||
readonly superclass: Superclass,
|
||||
readonly traits: Traits,
|
||||
readonly traits: T,
|
||||
) {}
|
||||
|
||||
get extends(): (
|
||||
AbstractClass<
|
||||
Pipe<Traits, [
|
||||
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
|
||||
@@ -49,7 +26,7 @@ export class TraitExpression<
|
||||
ConstructorParameters<Superclass>
|
||||
> &
|
||||
|
||||
Pipe<Traits, [
|
||||
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
|
||||
@@ -70,11 +47,29 @@ export class TraitExpression<
|
||||
>(
|
||||
this: This
|
||||
) {
|
||||
return new TraitBuilder(
|
||||
return new TraitBuilder<
|
||||
This,
|
||||
Simplify<
|
||||
ExtendPlain<Traits.MapAbstract<T>>
|
||||
>,
|
||||
Simplify<
|
||||
ExtendPlain<Traits.MapStaticAbstract<T>>
|
||||
>,
|
||||
(
|
||||
AbstractClass<
|
||||
Simplify<
|
||||
ExtendPlain<Traits.MapImplInstance<T>>
|
||||
>
|
||||
> &
|
||||
Simplify<
|
||||
ExtendPlain<Traits.MapImplStaticMembers<T>>
|
||||
>
|
||||
)
|
||||
>(
|
||||
this,
|
||||
{} as Simplify<SubtraitAbstract<This>>,
|
||||
{} as Simplify<SubtraitStaticAbstract<This>>,
|
||||
Super => class extends Super {},
|
||||
{} as any,
|
||||
{} as any,
|
||||
Super => class extends Super {} as any,
|
||||
)
|
||||
}
|
||||
}
|
||||
|
||||
Reference in New Issue
Block a user