Refactoring
All checks were successful
continuous-integration/drone/push Build is passing

This commit is contained in:
Julien Valverdé
2024-02-03 04:09:35 +01:00
parent 956457ee18
commit bdfc57481d
2 changed files with 37 additions and 44 deletions

View File

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