Trait refactoring
Some checks failed
continuous-integration/drone/push Build is failing

This commit is contained in:
Julien Valverdé
2024-02-03 23:15:30 +01:00
parent 8af604831b
commit 0d469fabeb
2 changed files with 19 additions and 8 deletions

View File

@@ -5,6 +5,6 @@ export type AbstractTag = "@thilawyn/traitify-ts/Abstract"
export function abstract< export function abstract<
Abstract extends {} = {} Abstract extends {} = {}
>(): Opaque<Abstract, AbstractTag> { >() {
return undefined as any return {} as Opaque<Abstract, AbstractTag>
} }

View File

@@ -30,15 +30,25 @@ type RemoveAbstractFromImpl<
export class Trait< export class Trait<
Abstract extends {}, Abstract extends {},
Impl extends Class<{}, []>, Impl extends Class<{}, []>,
Supertraits extends Trait<any, any, any>[]
> { > {
constructor( constructor(
readonly abstract: Abstract, readonly supertraits: Supertraits,
readonly ownAbstract: Abstract,
readonly apply: (Super: AbstractClass<{}>) => Impl, readonly apply: (Super: AbstractClass<{}>) => Impl,
) {} ) {}
} }
export interface Trait<
Abstract extends {},
Impl extends Class<{}, []>,
Supertraits extends Trait<any, any, any>[]
> {
get ownImplClass(): Impl
}
export type TraitAbstractMembers<T> = ( export type TraitAbstractMembers<T> = (
T extends Trait<infer AbstractMembers, any> T extends Trait<infer AbstractMembers, any, any>
? AbstractMembers ? AbstractMembers
: never : never
) )
@@ -48,7 +58,7 @@ export interface TraitAbstractMembersFn extends Fn {
} }
export type TraitImplClass<T> = ( export type TraitImplClass<T> = (
T extends Trait<any, infer Impl> T extends Trait<any, infer Impl, any>
? Impl ? Impl
: never : never
) )
@@ -102,6 +112,7 @@ export function trait<
), ),
) { ) {
return new Trait( return new Trait(
[] as const,
abstract as Abstract, abstract as Abstract,
apply as any as (Super: AbstractClass<{}>) => RemoveAbstractFromImpl<ImplWithAbstract, Abstract>, apply as any as (Super: AbstractClass<{}>) => RemoveAbstractFromImpl<ImplWithAbstract, Abstract>,
) )