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 { AbstractClass, Class } from "type-fest"
|
||||||
import { Trait, TraitClass, TraitConcreteClass } from "./Trait"
|
import { Trait, TraitClass, TraitConcreteClass } from "./Trait"
|
||||||
import { TraitExpression } from "./TraitExpression"
|
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, [
|
Pipe<SuperExpression, [
|
||||||
TraitExpression.TraitsFn,
|
TraitExpression.TraitsFn,
|
||||||
Tuples.Map<Trait.OwnAbstractFn>,
|
Tuples.Map<Trait.OwnAbstractFn>,
|
||||||
@@ -13,6 +19,12 @@ type ExtendAbstractSuper<SuperExpression, Abstract> = (
|
|||||||
ExtendFn,
|
ExtendFn,
|
||||||
SimplifyFn,
|
SimplifyFn,
|
||||||
]>
|
]>
|
||||||
|
// ExtendPlain<[
|
||||||
|
// ...Trait.OwnAbstract<
|
||||||
|
// TraitExpression.Traits<SuperExpression>[number]
|
||||||
|
// >,
|
||||||
|
// Abstract,
|
||||||
|
// ]>
|
||||||
)
|
)
|
||||||
|
|
||||||
type ExtendStaticAbstractSuper<SuperExpression, StaticAbstract> = (
|
type ExtendStaticAbstractSuper<SuperExpression, StaticAbstract> = (
|
||||||
@@ -30,19 +42,20 @@ type TraitApplierSuperTag = "@thilawyn/traitify-ts/Super"
|
|||||||
|
|
||||||
type RemoveAbstractFromImplClass<
|
type RemoveAbstractFromImplClass<
|
||||||
ImplClassWithAbstract extends AbstractClass<object>,
|
ImplClassWithAbstract extends AbstractClass<object>,
|
||||||
Abstract extends AbstractClass<object>,
|
Abstract extends object,
|
||||||
|
StaticAbstract extends object,
|
||||||
> = (
|
> = (
|
||||||
Class<
|
Class<
|
||||||
Omit<
|
Omit<
|
||||||
InstanceType<ImplClassWithAbstract>,
|
InstanceType<ImplClassWithAbstract>,
|
||||||
keyof InstanceType<Abstract>
|
keyof Abstract
|
||||||
>,
|
>,
|
||||||
|
|
||||||
ConstructorParameters<ImplClassWithAbstract>
|
ConstructorParameters<ImplClassWithAbstract>
|
||||||
> &
|
> &
|
||||||
Omit<
|
Omit<
|
||||||
StaticMembers<ImplClassWithAbstract>,
|
StaticMembers<ImplClassWithAbstract>,
|
||||||
keyof StaticMembers<Abstract> | "_tag"
|
keyof StaticAbstract | "_tag"
|
||||||
>
|
>
|
||||||
)
|
)
|
||||||
|
|
||||||
@@ -134,9 +147,8 @@ export class TraitBuilder<
|
|||||||
this.traitStaticAbstract,
|
this.traitStaticAbstract,
|
||||||
apply as unknown as (Super: AbstractClass<object>) => RemoveAbstractFromImplClass<
|
apply as unknown as (Super: AbstractClass<object>) => RemoveAbstractFromImplClass<
|
||||||
ImplClassWithAbstract,
|
ImplClassWithAbstract,
|
||||||
TraitClass<
|
ExtendAbstractSuper<SuperExpression, Abstract>,
|
||||||
Trait<SuperExpression, Abstract, StaticAbstract, ImplClass>
|
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 { trait } from "./TraitBuilder"
|
||||||
import { Implements, ImplementsStatic } from "./TraitExpression"
|
import { Implements, ImplementsStatic, TraitExpression } from "./TraitExpression"
|
||||||
import { expression } from "./TraitExpressionBuilder"
|
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
|
const PrintsHelloOnNew = trait
|
||||||
@@ -48,7 +66,7 @@ const StatefulSubscription = trait
|
|||||||
type StatefulSubscriptionClass = TraitClass<typeof StatefulSubscription>
|
type StatefulSubscriptionClass = TraitClass<typeof StatefulSubscription>
|
||||||
|
|
||||||
const ActiveStatefulSubscription = expression
|
const ActiveStatefulSubscription = expression
|
||||||
.expresses(StatefulSubscription)
|
.expresses(PrintsHelloOnNew, StatefulSubscription)
|
||||||
.build()
|
.build()
|
||||||
.subtrait()
|
.subtrait()
|
||||||
.extendAbstract(Super => class extends Super {
|
.extendAbstract(Super => class extends Super {
|
||||||
@@ -58,6 +76,10 @@ const ActiveStatefulSubscription = expression
|
|||||||
.build()
|
.build()
|
||||||
|
|
||||||
type ActiveStatefulSubscriptionClass = TraitClass<typeof ActiveStatefulSubscription>
|
type ActiveStatefulSubscriptionClass = TraitClass<typeof ActiveStatefulSubscription>
|
||||||
|
type Test = ExtendAbstractSuper<
|
||||||
|
Trait.OwnSuperExpression<typeof ActiveStatefulSubscription>,
|
||||||
|
Trait.OwnAbstract<typeof ActiveStatefulSubscription>
|
||||||
|
>
|
||||||
|
|
||||||
|
|
||||||
class TestSuperclass {
|
class TestSuperclass {
|
||||||
|
|||||||
@@ -12,7 +12,21 @@ interface ExtendReducerFn extends Fn {
|
|||||||
}
|
}
|
||||||
|
|
||||||
export type ExtendFn = Tuples.Reduce<ExtendReducerFn, {}>
|
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<[
|
export type ExtendableFn = ComposeLeft<[
|
||||||
@@ -22,4 +36,4 @@ export type ExtendableFn = ComposeLeft<[
|
|||||||
Match.With<any, true>,
|
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