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