This commit is contained in:
55
src/trait.ts
55
src/trait.ts
@@ -27,19 +27,29 @@ type RemoveAbstractFromImpl<
|
||||
)
|
||||
|
||||
|
||||
export type TraitTag = "@thilawyn/traitify-ts/Trait"
|
||||
// export type TraitTag = "@thilawyn/traitify-ts/Trait"
|
||||
|
||||
export type Trait<
|
||||
// export type Trait<
|
||||
// Abstract extends {},
|
||||
// Impl extends Class<{}, []>,
|
||||
// > = (
|
||||
// Opaque<{
|
||||
// readonly apply: TraitApplier<
|
||||
// Abstract,
|
||||
// AddAbstractToImpl<Impl, Abstract>
|
||||
// >
|
||||
// }, TraitTag>
|
||||
// )
|
||||
|
||||
export class Trait<
|
||||
Abstract extends {},
|
||||
Impl extends Class<{}, []>,
|
||||
> = (
|
||||
Opaque<{
|
||||
readonly apply: TraitApplier<
|
||||
Abstract,
|
||||
AddAbstractToImpl<Impl, Abstract>
|
||||
>
|
||||
}, TraitTag>
|
||||
)
|
||||
> {
|
||||
constructor(
|
||||
readonly abstract: Abstract,
|
||||
readonly apply: (Super: AbstractClass<{}>) => Impl,
|
||||
) {}
|
||||
}
|
||||
|
||||
export type TraitAbstractMembers<T> = (
|
||||
T extends Trait<infer AbstractMembers, any>
|
||||
@@ -96,24 +106,17 @@ export interface TraitInstanceFn extends Fn {
|
||||
|
||||
export type TraitApplierSuperTag = "@thilawyn/traitify-ts/TraitApplierSuper"
|
||||
|
||||
export type TraitApplier<
|
||||
Abstract extends {},
|
||||
ImplWithAbstract extends Class<Abstract, []>,
|
||||
> = (
|
||||
(Super: Opaque<AbstractClass<Abstract>, TraitApplierSuperTag>) => (
|
||||
Opaque<ImplWithAbstract, TraitApplierSuperTag>
|
||||
)
|
||||
)
|
||||
|
||||
export function trait<
|
||||
Abstract extends {},
|
||||
ImplWithAbstract extends Class<Abstract, []>,
|
||||
>(
|
||||
abstract: Opaque<Abstract, AbstractTag>,
|
||||
apply: TraitApplier<Abstract, ImplWithAbstract>,
|
||||
): Trait<
|
||||
Abstract,
|
||||
RemoveAbstractFromImpl<ImplWithAbstract, Abstract>
|
||||
> {
|
||||
return { apply } as any
|
||||
abstract: Opaque<Abstract, AbstractTag>,
|
||||
apply: (Super: Opaque<AbstractClass<Abstract>, TraitApplierSuperTag>) => (
|
||||
Opaque<ImplWithAbstract, TraitApplierSuperTag>
|
||||
),
|
||||
) {
|
||||
return new Trait(
|
||||
abstract as Abstract,
|
||||
apply as any as (Super: AbstractClass<{}>) => RemoveAbstractFromImpl<ImplWithAbstract, Abstract>,
|
||||
)
|
||||
}
|
||||
|
||||
Reference in New Issue
Block a user