Pure TS implementation attempt
All checks were successful
continuous-integration/drone/push Build is passing
All checks were successful
continuous-integration/drone/push Build is passing
This commit is contained in:
@@ -2,10 +2,16 @@ import { Pipe, Tuples } from "hotscript"
|
||||
import { AbstractClass, Class } from "type-fest"
|
||||
import { Trait, TraitClass, TraitConcreteClass } from "./Trait"
|
||||
import { TraitExpression } from "./TraitExpression"
|
||||
import { ExtendFn, SimplifyFn, StaticMembers } from "./util"
|
||||
import { ExtendFn, ExtendPlain, SimplifyFn, StaticMembers } from "./util"
|
||||
|
||||
|
||||
type ExtendAbstractSuper<SuperExpression, Abstract> = (
|
||||
export type ExtendAbstractSuper<
|
||||
SuperExpression extends TraitExpression<
|
||||
typeof TraitExpression.NullSuperclass,
|
||||
Trait<any, any, any, any>[]
|
||||
>,
|
||||
Abstract extends object,
|
||||
> = (
|
||||
Pipe<SuperExpression, [
|
||||
TraitExpression.TraitsFn,
|
||||
Tuples.Map<Trait.OwnAbstractFn>,
|
||||
@@ -13,6 +19,12 @@ type ExtendAbstractSuper<SuperExpression, Abstract> = (
|
||||
ExtendFn,
|
||||
SimplifyFn,
|
||||
]>
|
||||
// ExtendPlain<[
|
||||
// ...Trait.OwnAbstract<
|
||||
// TraitExpression.Traits<SuperExpression>[number]
|
||||
// >,
|
||||
// Abstract,
|
||||
// ]>
|
||||
)
|
||||
|
||||
type ExtendStaticAbstractSuper<SuperExpression, StaticAbstract> = (
|
||||
@@ -30,19 +42,20 @@ type TraitApplierSuperTag = "@thilawyn/traitify-ts/Super"
|
||||
|
||||
type RemoveAbstractFromImplClass<
|
||||
ImplClassWithAbstract extends AbstractClass<object>,
|
||||
Abstract extends AbstractClass<object>,
|
||||
Abstract extends object,
|
||||
StaticAbstract extends object,
|
||||
> = (
|
||||
Class<
|
||||
Omit<
|
||||
InstanceType<ImplClassWithAbstract>,
|
||||
keyof InstanceType<Abstract>
|
||||
keyof Abstract
|
||||
>,
|
||||
|
||||
ConstructorParameters<ImplClassWithAbstract>
|
||||
> &
|
||||
Omit<
|
||||
StaticMembers<ImplClassWithAbstract>,
|
||||
keyof StaticMembers<Abstract> | "_tag"
|
||||
keyof StaticAbstract | "_tag"
|
||||
>
|
||||
)
|
||||
|
||||
@@ -134,9 +147,8 @@ export class TraitBuilder<
|
||||
this.traitStaticAbstract,
|
||||
apply as unknown as (Super: AbstractClass<object>) => RemoveAbstractFromImplClass<
|
||||
ImplClassWithAbstract,
|
||||
TraitClass<
|
||||
Trait<SuperExpression, Abstract, StaticAbstract, ImplClass>
|
||||
>
|
||||
ExtendAbstractSuper<SuperExpression, Abstract>,
|
||||
ExtendStaticAbstractSuper<SuperExpression, StaticAbstract>
|
||||
>,
|
||||
)
|
||||
}
|
||||
|
||||
28
src/tests.ts
28
src/tests.ts
@@ -1,7 +1,25 @@
|
||||
import { TraitClass } from "./Trait"
|
||||
import { Trait, TraitClass } from "./Trait"
|
||||
import { trait } from "./TraitBuilder"
|
||||
import { Implements, ImplementsStatic } from "./TraitExpression"
|
||||
import { Implements, ImplementsStatic, TraitExpression } from "./TraitExpression"
|
||||
import { expression } from "./TraitExpressionBuilder"
|
||||
import { ExtendPlain } from "./util"
|
||||
|
||||
|
||||
type ExtendAbstractSuper<
|
||||
SuperExpression extends TraitExpression<
|
||||
typeof TraitExpression.NullSuperclass,
|
||||
Trait<any, any, any, any>[]
|
||||
>,
|
||||
Abstract extends object,
|
||||
> = (
|
||||
// ExtendPlain<[
|
||||
// ...Trait.OwnAbstract<
|
||||
// TraitExpression.Traits<SuperExpression>[number]
|
||||
// >,
|
||||
// Abstract,
|
||||
// ]>
|
||||
TraitExpression.Traits<SuperExpression>[number]
|
||||
)
|
||||
|
||||
|
||||
const PrintsHelloOnNew = trait
|
||||
@@ -48,7 +66,7 @@ const StatefulSubscription = trait
|
||||
type StatefulSubscriptionClass = TraitClass<typeof StatefulSubscription>
|
||||
|
||||
const ActiveStatefulSubscription = expression
|
||||
.expresses(StatefulSubscription)
|
||||
.expresses(PrintsHelloOnNew, StatefulSubscription)
|
||||
.build()
|
||||
.subtrait()
|
||||
.extendAbstract(Super => class extends Super {
|
||||
@@ -58,6 +76,10 @@ const ActiveStatefulSubscription = expression
|
||||
.build()
|
||||
|
||||
type ActiveStatefulSubscriptionClass = TraitClass<typeof ActiveStatefulSubscription>
|
||||
type Test = ExtendAbstractSuper<
|
||||
Trait.OwnSuperExpression<typeof ActiveStatefulSubscription>,
|
||||
Trait.OwnAbstract<typeof ActiveStatefulSubscription>
|
||||
>
|
||||
|
||||
|
||||
class TestSuperclass {
|
||||
|
||||
@@ -12,7 +12,21 @@ interface ExtendReducerFn extends Fn {
|
||||
}
|
||||
|
||||
export type ExtendFn = Tuples.Reduce<ExtendReducerFn, {}>
|
||||
export type Extend<T extends {}[]> = Call<ExtendFn, T>
|
||||
export type Extend<T extends readonly object[]> = Call<ExtendFn, T>
|
||||
|
||||
|
||||
export type ExtendPlain<T extends readonly any[]> = (
|
||||
T extends [infer Super, infer Self, ...infer Rest]
|
||||
? Pick<Self, CommonKeys<Self, Super>> extends Pick<Super, CommonKeys<Self, Super>>
|
||||
? ExtendPlain<[
|
||||
Omit<Super, CommonKeys<Self, Super>> & Self,
|
||||
...Rest,
|
||||
]>
|
||||
: never
|
||||
: T extends [infer Self]
|
||||
? Self
|
||||
: void
|
||||
)
|
||||
|
||||
|
||||
export type ExtendableFn = ComposeLeft<[
|
||||
@@ -22,4 +36,4 @@ export type ExtendableFn = ComposeLeft<[
|
||||
Match.With<any, true>,
|
||||
]>
|
||||
]>
|
||||
export type Extendable<T extends {}[]> = Call<ExtendableFn, T>
|
||||
export type Extendable<T extends object[]> = Call<ExtendableFn, T>
|
||||
|
||||
Reference in New Issue
Block a user