94 lines
2.5 KiB
TypeScript
94 lines
2.5 KiB
TypeScript
import { Trait, trait } from "./Trait"
|
|
import { Implements, ImplementsStatic } from "./TraitExpression"
|
|
import { expression } from "./TraitExpressionBuilder"
|
|
import { abstract } from "./abstract"
|
|
|
|
|
|
const PrintsHelloOnNew = trait(
|
|
abstract(),
|
|
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 }>(),
|
|
abstract(),
|
|
Super => class Identifiable extends Super {
|
|
equals(el: Identifiable) {
|
|
return this.id === el.id
|
|
}
|
|
},
|
|
)
|
|
|
|
const StatefulSubscription = trait(
|
|
abstract<{
|
|
readonly isStatefulSubscription: true
|
|
readonly status: (
|
|
{ _tag: "awaitingPayment" } |
|
|
{ _tag: "active", activeSince: Date, expiresAt?: Date } |
|
|
{ _tag: "expired", expiredSince: Date }
|
|
)
|
|
}>(),
|
|
abstract(),
|
|
|
|
Super => class StatefulSubscription extends Super {},
|
|
)
|
|
type StatefulSubscriptionClass = Trait.Class<typeof StatefulSubscription>
|
|
|
|
const ActiveStatefulSubscription = expression
|
|
.expresses(StatefulSubscription)
|
|
.build()
|
|
.subtrait(
|
|
exp => {
|
|
interface IActiveStatefulSubscription extends Implements<typeof exp> {
|
|
readonly isActiveStatefulSubscription: true
|
|
readonly status: { _tag: "active", activeSince: Date, expiresAt?: Date }
|
|
}
|
|
|
|
return abstract<IActiveStatefulSubscription>()
|
|
},
|
|
|
|
exp => abstract<ImplementsStatic<typeof exp>>(),
|
|
|
|
Super => class ActiveStatefulSubscription extends Super {},
|
|
)
|
|
|
|
type ActiveStatefulSubscriptionClass = Trait.Class<typeof ActiveStatefulSubscription>
|
|
|
|
|
|
class TestSuperclass {
|
|
// id: number = 69
|
|
// static test = 69
|
|
}
|
|
|
|
const exp = expression
|
|
.extends(TestSuperclass)
|
|
.expresses(
|
|
PrintsHelloOnNew,
|
|
Identifiable<bigint>(),
|
|
// Identifiable<number>(),
|
|
ActiveStatefulSubscription,
|
|
)
|
|
.build()
|
|
|
|
type Abs = Implements<typeof exp>
|
|
type AbsStatic = ImplementsStatic<typeof exp>
|
|
|
|
@exp.implementsStatic
|
|
class User extends exp.extends implements Implements<typeof exp> {
|
|
readonly isStatefulSubscription: true = true
|
|
readonly isActiveStatefulSubscription: true = true
|
|
declare status: { _tag: "active"; activeSince: Date; expiresAt?: Date | undefined }
|
|
id: bigint = -1n
|
|
}
|
|
|
|
console.log(new User())
|