This commit is contained in:
26
src/index.ts
26
src/index.ts
@@ -34,11 +34,16 @@ export type TraitApplier<
|
||||
* Unwraps the type of the class from a given trait.
|
||||
* @template T - The trait type.
|
||||
*/
|
||||
export type UnwrapTraitC<T> =
|
||||
export type TraitClass<T> =
|
||||
T extends Trait<infer C>
|
||||
? C
|
||||
: never
|
||||
|
||||
export type TraitInstance<T> =
|
||||
T extends Trait<infer C>
|
||||
? InstanceType<C>
|
||||
: never
|
||||
|
||||
|
||||
/**
|
||||
* Creates a trait using the provided trait applier function.
|
||||
@@ -90,7 +95,7 @@ export type UnwrapTraitC<T> =
|
||||
* trait(Parent => {
|
||||
* abstract class ImplementsIdentifiable extends extendsAndExpresses(
|
||||
* Parent,
|
||||
* [Identifiable<ID>()],
|
||||
* Identifiable<ID>(),
|
||||
* ) {
|
||||
* id: ID = defaultID
|
||||
*
|
||||
@@ -123,7 +128,10 @@ export function trait<
|
||||
* @example
|
||||
* Extends a superclass and applies traits:
|
||||
* ```ts
|
||||
* class User extends extendsAndExpresses(Entity, [Identifiable<bigint>(), Permissible]) {
|
||||
* class User extends extendsAndExpresses(Entity,
|
||||
* Identifiable<bigint>(),
|
||||
* Permissible,
|
||||
* ) {
|
||||
* readonly id: bigint
|
||||
*
|
||||
* constructor(id: bigint) {
|
||||
@@ -138,7 +146,7 @@ export function extendsAndExpresses<
|
||||
Traits extends readonly Trait<any>[],
|
||||
>(
|
||||
extend: C,
|
||||
traits: Traits,
|
||||
...traits: Traits
|
||||
) {
|
||||
return traits.reduce(
|
||||
(previous, trait) => trait(previous),
|
||||
@@ -147,10 +155,8 @@ export function extendsAndExpresses<
|
||||
AbstractClass<
|
||||
InstanceType<C> &
|
||||
UnionToIntersection<
|
||||
InstanceType<
|
||||
UnwrapTraitC<
|
||||
Traits[number]
|
||||
>
|
||||
TraitInstance<
|
||||
Traits[number]
|
||||
>
|
||||
>,
|
||||
|
||||
@@ -160,7 +166,7 @@ export function extendsAndExpresses<
|
||||
StaticMembers<C> &
|
||||
StaticMembers<
|
||||
UnionToIntersection<
|
||||
UnwrapTraitC<
|
||||
TraitClass<
|
||||
Traits[number]
|
||||
>
|
||||
>
|
||||
@@ -191,5 +197,5 @@ export function expresses<
|
||||
>(
|
||||
...traits: Traits
|
||||
) {
|
||||
return extendsAndExpresses(Object, traits)
|
||||
return extendsAndExpresses(Object, ...traits)
|
||||
}
|
||||
|
||||
@@ -23,7 +23,7 @@ const ImplementsIdentifiable = <ID>(defaultID: ID) =>
|
||||
trait(Parent => {
|
||||
abstract class ImplementsIdentifiable extends extendsAndExpresses(
|
||||
Parent,
|
||||
[Identifiable<ID>()],
|
||||
Identifiable<ID>(),
|
||||
) {
|
||||
id: ID = defaultID
|
||||
|
||||
|
||||
Reference in New Issue
Block a user