This commit is contained in:
14
src/Trait.ts
14
src/Trait.ts
@@ -11,7 +11,7 @@ export class Trait<
|
|||||||
>,
|
>,
|
||||||
Abstract extends object,
|
Abstract extends object,
|
||||||
StaticAbstract extends object,
|
StaticAbstract extends object,
|
||||||
ImplClass extends Class<object, []>,
|
ImplClass extends AbstractClass<object, []>,
|
||||||
> {
|
> {
|
||||||
constructor(
|
constructor(
|
||||||
readonly superExpression: SuperExpression,
|
readonly superExpression: SuperExpression,
|
||||||
@@ -96,6 +96,18 @@ export namespace Trait {
|
|||||||
}
|
}
|
||||||
|
|
||||||
export namespace Traits {
|
export namespace Traits {
|
||||||
|
export type MapAbstract<T> = {
|
||||||
|
[K in keyof T]: K extends keyof []
|
||||||
|
? T[K]
|
||||||
|
: Trait.OwnAbstract<T[K]>
|
||||||
|
}
|
||||||
|
|
||||||
|
export type MapStaticAbstract<T> = {
|
||||||
|
[K in keyof T]: K extends keyof []
|
||||||
|
? T[K]
|
||||||
|
: Trait.OwnStaticAbstract<T[K]>
|
||||||
|
}
|
||||||
|
|
||||||
export type MapImplClass<T> = {
|
export type MapImplClass<T> = {
|
||||||
[K in keyof T]: K extends keyof []
|
[K in keyof T]: K extends keyof []
|
||||||
? T[K]
|
? T[K]
|
||||||
|
|||||||
@@ -1,4 +1,4 @@
|
|||||||
import { AbstractClass, Class, Simplify } from "type-fest"
|
import { AbstractClass, Simplify } from "type-fest"
|
||||||
import { Trait, Traits } from "./Trait"
|
import { Trait, Traits } from "./Trait"
|
||||||
import { TraitExpression } from "./TraitExpression"
|
import { TraitExpression } from "./TraitExpression"
|
||||||
import { ExtendPlain, StaticMembers } from "./util"
|
import { ExtendPlain, StaticMembers } from "./util"
|
||||||
@@ -37,7 +37,7 @@ export class TraitBuilder<
|
|||||||
>,
|
>,
|
||||||
Abstract extends object,
|
Abstract extends object,
|
||||||
StaticAbstract extends object,
|
StaticAbstract extends object,
|
||||||
ImplClass extends Class<object, []>,
|
ImplClass extends AbstractClass<object, []>,
|
||||||
> {
|
> {
|
||||||
constructor(
|
constructor(
|
||||||
private readonly traitSuperExpression: SuperExpression,
|
private readonly traitSuperExpression: SuperExpression,
|
||||||
@@ -85,7 +85,7 @@ export class TraitBuilder<
|
|||||||
this.traitStaticAbstract,
|
this.traitStaticAbstract,
|
||||||
|
|
||||||
apply as unknown as (Super: AbstractClass<object>) => (
|
apply as unknown as (Super: AbstractClass<object>) => (
|
||||||
Class< // TODO: use abstract class instead
|
AbstractClass<
|
||||||
Simplify<
|
Simplify<
|
||||||
Omit<
|
Omit<
|
||||||
InstanceType<ImplClassWithAbstract>,
|
InstanceType<ImplClassWithAbstract>,
|
||||||
|
|||||||
@@ -1,45 +1,22 @@
|
|||||||
import { Fn, Pipe, Tuples } from "hotscript"
|
import { Fn, Pipe, Tuples } from "hotscript"
|
||||||
import { AbstractClass, Class, Simplify } from "type-fest"
|
import { AbstractClass, Class, Simplify } from "type-fest"
|
||||||
import { Trait } from "./Trait"
|
import { Trait, Traits } from "./Trait"
|
||||||
import { TraitBuilder } from "./TraitBuilder"
|
import { TraitBuilder } from "./TraitBuilder"
|
||||||
import { ExtendFn, ExtendPlain, SimplifyFn, StaticMembersFn } from "./util"
|
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<
|
export class TraitExpression<
|
||||||
Superclass extends AbstractClass<object>,
|
Superclass extends AbstractClass<object>,
|
||||||
const Traits extends Trait<any, any, any, any>[],
|
const T extends Trait<any, any, any, any>[],
|
||||||
> {
|
> {
|
||||||
constructor(
|
constructor(
|
||||||
readonly superclass: Superclass,
|
readonly superclass: Superclass,
|
||||||
readonly traits: Traits,
|
readonly traits: T,
|
||||||
) {}
|
) {}
|
||||||
|
|
||||||
get extends(): (
|
get extends(): (
|
||||||
AbstractClass<
|
AbstractClass<
|
||||||
Pipe<Traits, [
|
Pipe<T, [
|
||||||
Tuples.Map<Trait.OwnImplInstanceFn>, // Map all the traits to the instance of their implementation class
|
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
|
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
|
ExtendFn, // Reduce to a single instance that extends all the instances in the list
|
||||||
@@ -49,7 +26,7 @@ export class TraitExpression<
|
|||||||
ConstructorParameters<Superclass>
|
ConstructorParameters<Superclass>
|
||||||
> &
|
> &
|
||||||
|
|
||||||
Pipe<Traits, [
|
Pipe<T, [
|
||||||
Tuples.Map<Trait.OwnImplClassFn>, // Map all the traits to their implementation class
|
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.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
|
Tuples.Map<StaticMembersFn>, // Map all the classes to an object containing their static members
|
||||||
@@ -70,11 +47,29 @@ export class TraitExpression<
|
|||||||
>(
|
>(
|
||||||
this: This
|
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,
|
this,
|
||||||
{} as Simplify<SubtraitAbstract<This>>,
|
{} as any,
|
||||||
{} as Simplify<SubtraitStaticAbstract<This>>,
|
{} as any,
|
||||||
Super => class extends Super {},
|
Super => class extends Super {} as any,
|
||||||
)
|
)
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|||||||
Reference in New Issue
Block a user