This commit is contained in:
36
src/index.ts
36
src/index.ts
@@ -3,13 +3,15 @@ import { AbstractClass, AbstractConstructor, Opaque, UnionToIntersection } from
|
|||||||
|
|
||||||
/**
|
/**
|
||||||
* Represents the static members of a class.
|
* Represents the static members of a class.
|
||||||
*
|
* @template C - The class type.
|
||||||
* @template C - The type of the class for which static members are extracted.
|
|
||||||
* @typeparam The static members of the class.
|
|
||||||
*/
|
*/
|
||||||
export type StaticMembers<C> = Pick<C, keyof C>
|
export type StaticMembers<C> = Pick<C, keyof C>
|
||||||
|
|
||||||
|
|
||||||
|
/**
|
||||||
|
* Represents a trait that can be applied to a class.
|
||||||
|
* @template C - The abstract class type.
|
||||||
|
*/
|
||||||
export type Trait<
|
export type Trait<
|
||||||
C extends AbstractClass<any>
|
C extends AbstractClass<any>
|
||||||
> = Opaque<
|
> = Opaque<
|
||||||
@@ -17,17 +19,31 @@ export type Trait<
|
|||||||
"thilatrait/Trait"
|
"thilatrait/Trait"
|
||||||
>
|
>
|
||||||
|
|
||||||
|
/**
|
||||||
|
* Represents the function signature for applying a trait to a parent class.
|
||||||
|
* @template C - The abstract class type.
|
||||||
|
*/
|
||||||
export type TraitApplier<
|
export type TraitApplier<
|
||||||
C extends AbstractClass<any>
|
C extends AbstractClass<any>
|
||||||
> =
|
> =
|
||||||
(Parent: AbstractConstructor<any>) => C
|
(Parent: AbstractConstructor<any>) => C
|
||||||
|
|
||||||
|
/**
|
||||||
|
* Unwraps the type of the class from a given trait.
|
||||||
|
* @template T - The trait type.
|
||||||
|
*/
|
||||||
export type UnwrapTraitC<T> =
|
export type UnwrapTraitC<T> =
|
||||||
T extends Trait<infer C>
|
T extends Trait<infer C>
|
||||||
? C
|
? C
|
||||||
: never
|
: never
|
||||||
|
|
||||||
|
|
||||||
|
/**
|
||||||
|
* Creates a trait instance using the provided trait applier function.
|
||||||
|
* @template C - The abstract class type.
|
||||||
|
* @param applier - The trait applier function.
|
||||||
|
* @returns A trait instance.
|
||||||
|
*/
|
||||||
export function trait<
|
export function trait<
|
||||||
C extends AbstractClass<any>
|
C extends AbstractClass<any>
|
||||||
>(
|
>(
|
||||||
@@ -37,6 +53,14 @@ export function trait<
|
|||||||
}
|
}
|
||||||
|
|
||||||
|
|
||||||
|
/**
|
||||||
|
* Extends a class with the given traits and expresses their combined functionality.
|
||||||
|
* @template C - The abstract class type.
|
||||||
|
* @template Traits - An array of trait instances.
|
||||||
|
* @param extend - The class to extend.
|
||||||
|
* @param traits - An array of trait instances to apply.
|
||||||
|
* @returns A new class type expressing the combined functionality of the base class and traits.
|
||||||
|
*/
|
||||||
export function extendsAndExpresses<
|
export function extendsAndExpresses<
|
||||||
C extends AbstractClass<any>,
|
C extends AbstractClass<any>,
|
||||||
Traits extends readonly Trait<any>[],
|
Traits extends readonly Trait<any>[],
|
||||||
@@ -72,6 +96,12 @@ export function extendsAndExpresses<
|
|||||||
)
|
)
|
||||||
}
|
}
|
||||||
|
|
||||||
|
/**
|
||||||
|
* Expresses the combined functionality of multiple traits.
|
||||||
|
* @template Traits - An array of trait instances.
|
||||||
|
* @param traits - An array of trait instances to apply.
|
||||||
|
* @returns A new class type expressing the combined functionality of the traits.
|
||||||
|
*/
|
||||||
export function expresses<
|
export function expresses<
|
||||||
Traits extends readonly Trait<any>[],
|
Traits extends readonly Trait<any>[],
|
||||||
>(
|
>(
|
||||||
|
|||||||
Reference in New Issue
Block a user