0.1.0 #1

Merged
Thilawyn merged 65 commits from next into master 2024-02-06 03:15:40 +01:00
3 changed files with 43 additions and 33 deletions
Showing only changes of commit c1097ff2f2 - Show all commits

View File

@@ -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
}

View File

@@ -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 {},
)

View File

@@ -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
}