Co-authored-by: Julien Valverdé <julien.valverde@mailo.com> Reviewed-on: https://git.jvalver.de/Thilawyn/traitify-ts/pulls/4
This commit was merged in pull request #4.
This commit is contained in:
77
src/tests.ts
77
src/tests.ts
@@ -1,67 +1,61 @@
|
||||
import { Trait, trait } from "./Trait"
|
||||
import { Implements } from "./TraitExpression"
|
||||
import { TraitClass } from "./Trait"
|
||||
import { trait } from "./TraitBuilder"
|
||||
import { Implements, ImplementsStatic, TraitExpressionClass } from "./TraitExpression"
|
||||
import { expression } from "./TraitExpressionBuilder"
|
||||
import { abstract } from "./abstract"
|
||||
|
||||
|
||||
const PrintsHelloOnNew = trait(
|
||||
abstract(),
|
||||
Super => class PrintsHelloOnNew extends Super {
|
||||
const PrintsHelloOnNew = trait
|
||||
.implement(Super => class PrintsHelloOnNew extends Super {
|
||||
static readonly isPrintsHelloOnNew = true
|
||||
|
||||
constructor(...args: any[]) {
|
||||
super(...args)
|
||||
console.log("Hello!")
|
||||
}
|
||||
},
|
||||
)
|
||||
type PrintsHelloOnNewClass = Trait.Class<typeof PrintsHelloOnNew>
|
||||
})
|
||||
.build()
|
||||
|
||||
const Identifiable = <ID>() => trait(
|
||||
abstract<{ readonly id: ID }>(),
|
||||
Super => class Identifiable extends Super {
|
||||
type PrintsHelloOnNewClass = TraitClass<typeof PrintsHelloOnNew>
|
||||
|
||||
const Identifiable = <ID>() => trait
|
||||
.abstract(Super => class extends Super {
|
||||
declare readonly id: ID
|
||||
})
|
||||
.implement(Super => class Identifiable extends Super {
|
||||
equals(el: Identifiable) {
|
||||
return this.id === el.id
|
||||
}
|
||||
},
|
||||
)
|
||||
})
|
||||
.build()
|
||||
|
||||
const StatefulSubscription = trait(
|
||||
abstract<{
|
||||
readonly isStatefulSubscription: true
|
||||
readonly status: (
|
||||
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()
|
||||
|
||||
Super => class StatefulSubscription extends Super {},
|
||||
)
|
||||
type StatefulSubscriptionClass = Trait.Class<typeof StatefulSubscription>
|
||||
type StatefulSubscriptionClass = TraitClass<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>()
|
||||
},
|
||||
|
||||
Super => class ActiveStatefulSubscription extends Super {},
|
||||
)
|
||||
|
||||
type ActiveStatefulSubscriptionClass = Trait.Class<typeof ActiveStatefulSubscription>
|
||||
.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
|
||||
static test = 69
|
||||
}
|
||||
|
||||
const exp = expression
|
||||
@@ -75,7 +69,10 @@ const exp = expression
|
||||
.build()
|
||||
|
||||
type Abs = Implements<typeof exp>
|
||||
type AbsStatic = ImplementsStatic<typeof exp>
|
||||
type ExpClass = TraitExpressionClass<typeof exp>
|
||||
|
||||
@exp.implementsStatic
|
||||
class User extends exp.extends implements Implements<typeof exp> {
|
||||
readonly isStatefulSubscription: true = true
|
||||
readonly isActiveStatefulSubscription: true = true
|
||||
@@ -84,3 +81,9 @@ class User extends exp.extends implements Implements<typeof exp> {
|
||||
}
|
||||
|
||||
console.log(new User())
|
||||
|
||||
|
||||
// type T = NonExtendableKeys<[
|
||||
// { prout: "gneugneu" },
|
||||
// { prout: string },
|
||||
// ]>
|
||||
|
||||
Reference in New Issue
Block a user