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

This commit is contained in:
Julien Valverdé
2024-02-19 03:57:23 +01:00
parent 2e716e7f96
commit 4ba0a19037
2 changed files with 22 additions and 23 deletions

View File

@@ -1,8 +1,8 @@
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, TraitInstance, Traits } from "./Trait" import { Trait, TraitInstance, TraitStaticMembers, Traits } from "./Trait"
import { TraitBuilder } from "./TraitBuilder" import { TraitBuilder } from "./TraitBuilder"
import { ExtendFn, ExtendPlain, SimplifyFn, StaticMembersFn } from "./util" import { ExtendFn, ExtendPlain, SimplifyFn, StaticMembers, StaticMembersFn } from "./util"
export class TraitExpression< export class TraitExpression<
@@ -148,37 +148,35 @@ export type TraitExpressionConcreteClass<
export type TraitExpressionInstance< export type TraitExpressionInstance<
Exp extends TraitExpression<any, Trait<any, any, any, any>[]> Exp extends TraitExpression<any, Trait<any, any, any, any>[]>
> = ( > = (
// Pipe<TraitExpression.Traits<Exp>, [
// Tuples.Map<Trait.OwnInstanceFn>, // Map all the traits to their instance representation
// Tuples.Prepend< // Add the instance of the superclass at the top of the list
// InstanceType<TraitExpression.Superclass<Exp>>
// >,
// ExtendFn, // Reduce to a single object that extends all the objects in the list
// SimplifyFn, // Make readable for IDEs
// ]>
Simplify< Simplify<
ExtendPlain<[ ExtendPlain<[
InstanceType<TraitExpression.Superclass<Exp>>, InstanceType<TraitExpression.Superclass<Exp>>,
...MapTraitsInstance< ...MapTraitsInstance<TraitExpression.Traits<Exp>>,
TraitExpression.Traits<Exp>
>,
]> ]>
> >
) )
type MapTraitsInstance<T extends Trait<any, any, any, any>[]> = { type MapTraitsInstance<T> = {
[K in keyof T]: K extends keyof [] [K in keyof T]: K extends keyof []
? T[K] ? T[K]
: TraitInstance<T[K]> : T[K] extends Trait<any, any, any, any>
? TraitInstance<T[K]>
: never
} }
export type TraitExpressionStaticMembers< export type TraitExpressionStaticMembers<
Exp extends TraitExpression<any, Trait<any, any, any, any>[]> Exp extends TraitExpression<any, Trait<any, any, any, any>[]>
> = ( > = (
Pipe<TraitExpression.Traits<Exp>, [ Simplify<
Tuples.Map<Trait.OwnClassFn>, // Map all the traits to their class representation ExtendPlain<[
Tuples.Prepend<TraitExpression.Superclass<Exp>>, // Add the superclass at the top of the list StaticMembers<TraitExpression.Superclass<Exp>>,
Tuples.Map<StaticMembersFn>, // Map all the classes to an object containing their static members ...MapTraitsStaticMembers<TraitExpression.Traits<Exp>>,
ExtendFn, // Reduce to a single object that extends all the objects in the list
SimplifyFn, // Make readable for IDEs
]> ]>
>
) )
type MapTraitsStaticMembers<T> = {
[K in keyof T]: K extends keyof []
? T[K]
: T[K] extends Trait<any, any, any, any>
? TraitStaticMembers<T[K]>
: never
}

View File

@@ -1,6 +1,6 @@
import { TraitClass } from "./Trait" import { TraitClass } from "./Trait"
import { trait } from "./TraitBuilder" import { trait } from "./TraitBuilder"
import { Implements, ImplementsStatic } from "./TraitExpression" import { Implements, ImplementsStatic, TraitExpressionClass } from "./TraitExpression"
import { expression } from "./TraitExpressionBuilder" import { expression } from "./TraitExpressionBuilder"
@@ -70,6 +70,7 @@ const exp = expression
type Abs = Implements<typeof exp> type Abs = Implements<typeof exp>
type AbsStatic = ImplementsStatic<typeof exp> type AbsStatic = ImplementsStatic<typeof exp>
type ExpClass = TraitExpressionClass<typeof exp>
@exp.implementsStatic @exp.implementsStatic
class User extends exp.extends implements Implements<typeof exp> { class User extends exp.extends implements Implements<typeof exp> {