All checks were successful
continuous-integration/drone/push Build is passing
Co-authored-by: Julien Valverdé <julien.valverde@mailo.com> Reviewed-on: https://git.jvalver.de/Thilawyn/traitify-ts/pulls/24
98 lines
2.8 KiB
TypeScript
98 lines
2.8 KiB
TypeScript
import { TraitClass } from "./Trait"
|
|
import { trait } from "./TraitBuilder"
|
|
import { Implements, StaticImplements, TraitExpressionClass } from "./TraitExpression"
|
|
import { expression } from "./TraitExpressionBuilder"
|
|
import { type } from "./util"
|
|
|
|
|
|
const PrintsHelloOnNew = trait
|
|
.implement(Super => class PrintsHelloOnNew extends Super {
|
|
static readonly isPrintsHelloOnNew = true
|
|
|
|
constructor(...args: any[]) {
|
|
super(...args)
|
|
console.log("Hello!")
|
|
}
|
|
})
|
|
.build()
|
|
|
|
type PrintsHelloOnNewClass = TraitClass<typeof PrintsHelloOnNew>
|
|
|
|
const Identifiable = <ID>() => trait
|
|
// .abstract(Super => class extends Super {
|
|
// declare readonly id: ID
|
|
// })
|
|
.abstractType(Super => type<typeof Super & {
|
|
readonly id: ID
|
|
}>)
|
|
.implement(Super => class Identifiable extends Super {
|
|
equals(el: Identifiable) {
|
|
return this.id === el.id
|
|
}
|
|
})
|
|
.build()
|
|
|
|
const ImplementsIdentifiable = <ID>(defaultID: ID) => expression
|
|
.expresses(Identifiable<ID>())
|
|
.build()
|
|
.subtrait()
|
|
.implement(Super => class ImplementsIdentifiable extends Super {
|
|
readonly id = defaultID
|
|
})
|
|
.build()
|
|
|
|
const StatefulSubscription = trait
|
|
.abstract(Super => class extends Super {
|
|
declare readonly isStatefulSubscription: true
|
|
declare readonly status: (
|
|
{ _tag: "awaitingPayment" } |
|
|
{ _tag: "active", activeSince: Date, expiresAt?: Date } |
|
|
{ _tag: "expired", expiredSince: Date }
|
|
)
|
|
})
|
|
.build()
|
|
|
|
type StatefulSubscriptionClass = TraitClass<typeof StatefulSubscription>
|
|
|
|
const ActiveStatefulSubscription = expression
|
|
.expresses(StatefulSubscription)
|
|
.build()
|
|
.subtrait()
|
|
.abstract(Super => class extends Super {
|
|
declare readonly isActiveStatefulSubscription: true
|
|
declare readonly status: { _tag: "active", activeSince: Date, expiresAt?: Date }
|
|
})
|
|
.build()
|
|
|
|
type ActiveStatefulSubscriptionClass = TraitClass<typeof ActiveStatefulSubscription>
|
|
|
|
class TestSuperclass {
|
|
// id: number = 69
|
|
static test = 69
|
|
}
|
|
|
|
const exp = expression
|
|
.extends(TestSuperclass)
|
|
.expresses(
|
|
PrintsHelloOnNew,
|
|
Identifiable<bigint>(),
|
|
// Identifiable<number>(),
|
|
// StatefulSubscription,
|
|
ActiveStatefulSubscription,
|
|
)
|
|
.build()
|
|
|
|
type Abs = Implements<typeof exp>
|
|
type AbsStatic = StaticImplements<typeof exp>
|
|
type ExpClass = TraitExpressionClass<typeof exp>
|
|
|
|
@exp.staticImplements
|
|
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())
|