This commit is contained in:
12
src/Trait.ts
12
src/Trait.ts
@@ -1,5 +1,5 @@
|
||||
import { Fn, Pipe, Tuples } from "hotscript"
|
||||
import { AbstractClass, Class, Opaque } from "type-fest"
|
||||
import { AbstractClass, Class, Opaque, UnwrapOpaque } from "type-fest"
|
||||
import { AbstractTag, TraitExpression, emptyTraitExpression } from "."
|
||||
import { ExtendFn, SimplifyFn, StaticMembers, StaticMembersFn } from "./util"
|
||||
|
||||
@@ -16,14 +16,14 @@ export type AddAbstractToImplClass<
|
||||
)
|
||||
|
||||
export type RemoveAbstractFromImplClass<
|
||||
ImplClassWithAbstract extends Class<Abstract, []>,
|
||||
ImplClassWithAbstract extends Opaque<Class<Abstract, []>, TraitApplierSuperTag>,
|
||||
Abstract extends {},
|
||||
> = (
|
||||
Class<
|
||||
Omit<InstanceType<ImplClassWithAbstract>, keyof Abstract>,
|
||||
ConstructorParameters<ImplClassWithAbstract>
|
||||
> &
|
||||
StaticMembers<ImplClassWithAbstract>
|
||||
UnwrapOpaque<StaticMembers<ImplClassWithAbstract>>
|
||||
)
|
||||
|
||||
|
||||
@@ -138,12 +138,10 @@ export type TraitApplierSuperTag = "@thilawyn/traitify-ts/TraitApplierSuper"
|
||||
|
||||
export function trait<
|
||||
Abstract extends {},
|
||||
ImplClassWithAbstract extends Class<Abstract, []>,
|
||||
ImplClassWithAbstract extends Opaque<Class<Abstract, []>, TraitApplierSuperTag>,
|
||||
>(
|
||||
abstract: Opaque<Abstract, AbstractTag>,
|
||||
apply: (Super: Opaque<AbstractClass<Abstract>, TraitApplierSuperTag>) => (
|
||||
Opaque<ImplClassWithAbstract, TraitApplierSuperTag>
|
||||
),
|
||||
apply: (Super: Opaque<AbstractClass<Abstract>, TraitApplierSuperTag>) => ImplClassWithAbstract,
|
||||
) {
|
||||
return new Trait(
|
||||
emptyTraitExpression,
|
||||
|
||||
@@ -13,8 +13,8 @@ import { ExtendFn, SimplifyFn, StaticMembersFn } from "./util"
|
||||
|
||||
export class TraitExpression<
|
||||
Superclass extends AbstractClass<{}>,
|
||||
OwnTraits extends Trait<any, any, any>[],
|
||||
AllTraits extends Trait<any, any, any>[],
|
||||
const OwnTraits extends Trait<any, any, any>[],
|
||||
const AllTraits extends Trait<any, any, any>[],
|
||||
> {
|
||||
constructor(
|
||||
readonly superclass: Superclass,
|
||||
@@ -51,13 +51,13 @@ export class TraitExpression<
|
||||
}
|
||||
|
||||
subtrait<
|
||||
SubtraitAbstract extends Implements<typeof this>,
|
||||
SubtraitImplClassWithAbstract extends Class<SubtraitAbstract, []>,
|
||||
This extends TraitExpression<typeof TraitExpression.NullSuperclass, any, any>,
|
||||
SubtraitAbstract extends Implements<This>,
|
||||
SubtraitImplClassWithAbstract extends Opaque<Class<SubtraitAbstract, []>, TraitApplierSuperTag>,
|
||||
>(
|
||||
abstract: (expression: typeof this) => Opaque<SubtraitAbstract, AbstractTag>,
|
||||
apply: (Super: Opaque<AbstractClass<SubtraitAbstract>, TraitApplierSuperTag>) => (
|
||||
Opaque<SubtraitImplClassWithAbstract, TraitApplierSuperTag>
|
||||
),
|
||||
this: This,
|
||||
abstract: (expression: This) => Opaque<SubtraitAbstract, AbstractTag>,
|
||||
apply: (Super: Opaque<AbstractClass<SubtraitAbstract>, TraitApplierSuperTag>) => SubtraitImplClassWithAbstract,
|
||||
) {
|
||||
return new Trait(
|
||||
this,
|
||||
@@ -99,11 +99,7 @@ export namespace TraitExpression {
|
||||
}
|
||||
}
|
||||
|
||||
export const emptyTraitExpression = new TraitExpression(
|
||||
TraitExpression.NullSuperclass,
|
||||
[] as const,
|
||||
[] as const,
|
||||
)
|
||||
export const emptyTraitExpression = new TraitExpression(TraitExpression.NullSuperclass, [], [])
|
||||
|
||||
|
||||
export type Implements<Exp extends TraitExpression<any, any, any>> = (
|
||||
|
||||
@@ -66,11 +66,11 @@ type BuildTraitExpression<
|
||||
|
||||
|
||||
class TraitExpressionBuilder<
|
||||
Super extends AbstractClass<{}>,
|
||||
OwnTraits extends Trait<any, any, any>[],
|
||||
AllTraits extends Trait<any, any, any>[],
|
||||
Superclass extends AbstractClass<{}>,
|
||||
const OwnTraits extends Trait<any, any, any>[],
|
||||
const AllTraits extends Trait<any, any, any>[],
|
||||
> {
|
||||
constructor(private expression: TraitExpression<Super, OwnTraits, AllTraits>) {}
|
||||
constructor(private expression: TraitExpression<Superclass, OwnTraits, AllTraits>) {}
|
||||
|
||||
extends<
|
||||
Super extends AbstractClass<any>
|
||||
@@ -87,25 +87,25 @@ class TraitExpressionBuilder<
|
||||
}
|
||||
|
||||
expresses<
|
||||
Traits extends Trait<any, any, any>[]
|
||||
const Traits extends Trait<any, any, any>[]
|
||||
>(
|
||||
...traits: Traits
|
||||
): TraitExpressionBuilder<
|
||||
Super,
|
||||
Superclass,
|
||||
[...OwnTraits, ...Traits],
|
||||
[...AllTraits, ...SpreadSupertraits<Traits>]
|
||||
> {
|
||||
return new TraitExpressionBuilder(
|
||||
new TraitExpression(
|
||||
this.expression.superclass,
|
||||
[...this.expression.ownTraits, ...traits] as const,
|
||||
[...this.expression.allTraits, ...this.spreadSupertraits(traits)] as const,
|
||||
[...this.expression.ownTraits, ...traits],
|
||||
[...this.expression.allTraits, ...this.spreadSupertraits(traits)],
|
||||
)
|
||||
)
|
||||
) as any
|
||||
}
|
||||
|
||||
private spreadSupertraits<
|
||||
Traits extends Trait<
|
||||
const Traits extends Trait<
|
||||
TraitExpression<any, any, Trait<any, any, any>[]>,
|
||||
any,
|
||||
any
|
||||
@@ -120,7 +120,7 @@ class TraitExpressionBuilder<
|
||||
}
|
||||
|
||||
build() {
|
||||
return this.expression as BuildTraitExpression<Super, OwnTraits, AllTraits>
|
||||
return this.expression as BuildTraitExpression<Superclass, OwnTraits, AllTraits>
|
||||
}
|
||||
|
||||
then<V>(fn: (expression: ReturnType<typeof this.build>) => V): V {
|
||||
|
||||
@@ -1,3 +1,8 @@
|
||||
export {
|
||||
AbstractTag, Implements, TraitApplierSuperTag, abstract, expression, trait, type Trait, type TraitExpression
|
||||
Implements,
|
||||
abstract,
|
||||
expression,
|
||||
trait,
|
||||
type Trait,
|
||||
type TraitExpression,
|
||||
} from "."
|
||||
|
||||
@@ -1,17 +1,18 @@
|
||||
import { Simplify } from "type-fest"
|
||||
import { Implements, Trait, abstract, expression, trait } from "."
|
||||
import { Pipe } from "hotscript"
|
||||
|
||||
|
||||
const PrintsHelloOnNew = trait(
|
||||
abstract(),
|
||||
Super => class PrintsHelloOnNew extends Super {
|
||||
static readonly isPrintsHelloOnNew = true
|
||||
|
||||
constructor(...args: any[]) {
|
||||
super(...args)
|
||||
console.log("Hello!")
|
||||
}
|
||||
},
|
||||
)
|
||||
type PrintsHelloOnNewClass = Trait.Class<typeof PrintsHelloOnNew>
|
||||
|
||||
const Identifiable = <ID>() => trait(
|
||||
abstract<{ readonly id: ID }>(),
|
||||
@@ -34,6 +35,7 @@ const StatefulSubscription = trait(
|
||||
|
||||
Super => class StatefulSubscription extends Super {},
|
||||
)
|
||||
type StatefulSubscriptionClass = Trait.Class<typeof StatefulSubscription>
|
||||
|
||||
const ActiveStatefulSubscription = expression
|
||||
.expresses(StatefulSubscription)
|
||||
@@ -51,7 +53,7 @@ const ActiveStatefulSubscription = expression
|
||||
Super => class ActiveStatefulSubscription extends Super {},
|
||||
)
|
||||
|
||||
type T = Trait.Instance<typeof ActiveStatefulSubscription>
|
||||
// type T = Trait.Instance<typeof ActiveStatefulSubscription>
|
||||
|
||||
|
||||
class TestSuperclass {
|
||||
|
||||
Reference in New Issue
Block a user