This commit is contained in:
@@ -3,6 +3,10 @@ import { Opaque } from "type-fest"
|
||||
|
||||
export type AbstractTag = "@thilawyn/traitify-ts/Abstract"
|
||||
|
||||
export function abstract<Abstract extends object = {}>() {
|
||||
return {} as Opaque<Abstract, AbstractTag>
|
||||
export function abstract<
|
||||
Abstract extends {} = {}
|
||||
>(): (
|
||||
Opaque<Abstract, AbstractTag>
|
||||
) {
|
||||
return undefined as any
|
||||
}
|
||||
|
||||
45
src/tests.ts
45
src/tests.ts
@@ -1,41 +1,44 @@
|
||||
import { Implements, TraitAbstractMembers, expression, trait } from "."
|
||||
import { Implements, TraitAbstractMembers, abstract, expression, trait } from "."
|
||||
|
||||
|
||||
const PrintsHelloOnNew = trait()(Super =>
|
||||
class PrintsHelloOnNew extends Super {
|
||||
const PrintsHelloOnNew = trait(
|
||||
abstract(),
|
||||
Super => class PrintsHelloOnNew extends Super {
|
||||
constructor(...args: any[]) {
|
||||
super(...args)
|
||||
console.log("Hello!")
|
||||
}
|
||||
}
|
||||
},
|
||||
)
|
||||
|
||||
const Identifiable = <ID>() => (
|
||||
trait<{ readonly id: ID }>()(Super =>
|
||||
class Identifiable extends Super {
|
||||
equals(el: Identifiable) {
|
||||
return this.id === el.id
|
||||
}
|
||||
const Identifiable = <ID>() => trait(
|
||||
abstract<{ readonly id: ID }>(),
|
||||
Super => class Identifiable extends Super {
|
||||
equals(el: Identifiable) {
|
||||
return this.id === el.id
|
||||
}
|
||||
)
|
||||
},
|
||||
)
|
||||
|
||||
const StatefulSubscription = trait<{
|
||||
readonly status: (
|
||||
{ _tag: "awaitingPayment" } |
|
||||
{ _tag: "active", activeSince: Date, expiresAt?: Date } |
|
||||
{ _tag: "expired", expiredSince: Date }
|
||||
)
|
||||
}>()(Super =>
|
||||
class StatefulSubscription extends Super {}
|
||||
const StatefulSubscription = trait(
|
||||
abstract<{
|
||||
readonly status: (
|
||||
{ _tag: "awaitingPayment" } |
|
||||
{ _tag: "active", activeSince: Date, expiresAt?: Date } |
|
||||
{ _tag: "expired", expiredSince: Date }
|
||||
)
|
||||
}>(),
|
||||
|
||||
Super => class StatefulSubscription extends Super {},
|
||||
)
|
||||
|
||||
interface ActiveStatefulSubscriptionAbstractMembers extends TraitAbstractMembers<typeof StatefulSubscription> {
|
||||
readonly status: { _tag: "active", activeSince: Date, expiresAt?: Date }
|
||||
}
|
||||
|
||||
const ActiveStatefulSubscription = trait<ActiveStatefulSubscriptionAbstractMembers>()(Super =>
|
||||
class ActiveStatefulSubscription extends Super {}
|
||||
const ActiveStatefulSubscription = trait(
|
||||
abstract<ActiveStatefulSubscriptionAbstractMembers>(),
|
||||
Super => class ActiveStatefulSubscription extends Super {},
|
||||
)
|
||||
|
||||
|
||||
|
||||
23
src/trait.ts
23
src/trait.ts
@@ -1,6 +1,7 @@
|
||||
import { Fn } from "hotscript"
|
||||
import { AbstractClass, Class, Opaque } from "type-fest"
|
||||
import { StaticMembers } from "./util"
|
||||
import { AbstractTag } from "."
|
||||
|
||||
|
||||
type AddAbstractToImpl<
|
||||
@@ -105,14 +106,16 @@ export type TraitApplier<
|
||||
)
|
||||
|
||||
export function trait<
|
||||
Abstract extends object = {}
|
||||
>(): (
|
||||
<ImplWithAbstract extends Class<Abstract, []>>(
|
||||
apply: TraitApplier<Abstract, ImplWithAbstract>
|
||||
) => Trait<
|
||||
Abstract,
|
||||
RemoveAbstractFromImpl<ImplWithAbstract, Abstract>
|
||||
>
|
||||
) {
|
||||
return apply => ({ apply }) as any
|
||||
Abstract extends {},
|
||||
ImplWithAbstract extends Class<Abstract, []>,
|
||||
>(
|
||||
abstract: Opaque<Abstract, AbstractTag>,
|
||||
apply: (Super: Opaque<AbstractClass<Abstract>, TraitApplierSuperTag>) => (
|
||||
Opaque<ImplWithAbstract, TraitApplierSuperTag>
|
||||
),
|
||||
): Trait<
|
||||
Abstract,
|
||||
RemoveAbstractFromImpl<ImplWithAbstract, Abstract>
|
||||
> {
|
||||
return { apply } as any
|
||||
}
|
||||
|
||||
Reference in New Issue
Block a user