This commit is contained in:
71
src/index.ts
71
src/index.ts
@@ -39,10 +39,45 @@ export type UnwrapTraitC<T> =
|
|||||||
|
|
||||||
|
|
||||||
/**
|
/**
|
||||||
* Creates a trait instance using the provided trait applier function.
|
* Creates a trait using the provided trait applier function.
|
||||||
* @template C - The abstract class type.
|
* @template C - The abstract class type.
|
||||||
* @param applier - The trait applier function.
|
* @param applier - The trait applier function.
|
||||||
* @returns A trait instance.
|
* @returns A trait.
|
||||||
|
* @example
|
||||||
|
* Creates a trait:
|
||||||
|
* ```ts
|
||||||
|
* const Permissible = trait(Parent => {
|
||||||
|
* abstract class Permissible extends Parent {
|
||||||
|
* static readonly defaultPermissions: string[] = []
|
||||||
|
* permissions: string[] = []
|
||||||
|
*
|
||||||
|
* constructor(...args: any[]) {
|
||||||
|
* super(...args)
|
||||||
|
* }
|
||||||
|
* }
|
||||||
|
*
|
||||||
|
* return Permissible
|
||||||
|
* })
|
||||||
|
* ```
|
||||||
|
* Creates a generic trait:
|
||||||
|
* ```ts
|
||||||
|
* const Identifiable = <ID>() =>
|
||||||
|
* trait(Parent => {
|
||||||
|
* abstract class Identifiable extends Parent {
|
||||||
|
* abstract readonly id: ID
|
||||||
|
*
|
||||||
|
* equals(el: Identifiable) {
|
||||||
|
* return this.id === el.id
|
||||||
|
* }
|
||||||
|
*
|
||||||
|
* constructor(...args: any[]) {
|
||||||
|
* super(...args)
|
||||||
|
* }
|
||||||
|
* }
|
||||||
|
*
|
||||||
|
* return Identifiable
|
||||||
|
* })
|
||||||
|
* ```
|
||||||
*/
|
*/
|
||||||
export function trait<
|
export function trait<
|
||||||
C extends AbstractClass<any>
|
C extends AbstractClass<any>
|
||||||
@@ -56,10 +91,22 @@ export function trait<
|
|||||||
/**
|
/**
|
||||||
* Extends a class with the given traits and expresses their combined functionality.
|
* Extends a class with the given traits and expresses their combined functionality.
|
||||||
* @template C - The abstract class type.
|
* @template C - The abstract class type.
|
||||||
* @template Traits - An array of trait instances.
|
* @template Traits - An array of traits.
|
||||||
* @param extend - The class to extend.
|
* @param extend - The class to extend.
|
||||||
* @param traits - An array of trait instances to apply.
|
* @param traits - An array of traits to apply.
|
||||||
* @returns A new class type expressing the combined functionality of the base class and traits.
|
* @returns A new class type expressing the combined functionality of the base class and traits.
|
||||||
|
* @example
|
||||||
|
* Extends a superclass and applies traits:
|
||||||
|
* ```ts
|
||||||
|
* class User extends extendsAndExpresses(Entity, [Identifiable<bigint>(), Permissible]) {
|
||||||
|
* readonly id: bigint
|
||||||
|
*
|
||||||
|
* constructor(id: bigint) {
|
||||||
|
* super()
|
||||||
|
* this.id = id
|
||||||
|
* }
|
||||||
|
* }
|
||||||
|
* ```
|
||||||
*/
|
*/
|
||||||
export function extendsAndExpresses<
|
export function extendsAndExpresses<
|
||||||
C extends AbstractClass<any>,
|
C extends AbstractClass<any>,
|
||||||
@@ -98,9 +145,21 @@ export function extendsAndExpresses<
|
|||||||
|
|
||||||
/**
|
/**
|
||||||
* Expresses the combined functionality of multiple traits.
|
* Expresses the combined functionality of multiple traits.
|
||||||
* @template Traits - An array of trait instances.
|
* @template Traits - An array of trait.
|
||||||
* @param traits - An array of trait instances to apply.
|
* @param traits - An array of trait to apply.
|
||||||
* @returns A new class type expressing the combined functionality of the traits.
|
* @returns A new class type expressing the combined functionality of the traits.
|
||||||
|
* @example
|
||||||
|
* Applies traits to a class:
|
||||||
|
* ```ts
|
||||||
|
* class User extends expresses(Identifiable<bigint>(), Permissible) {
|
||||||
|
* readonly id: bigint
|
||||||
|
*
|
||||||
|
* constructor(id: bigint) {
|
||||||
|
* super()
|
||||||
|
* this.id = id
|
||||||
|
* }
|
||||||
|
* }
|
||||||
|
* ```
|
||||||
*/
|
*/
|
||||||
export function expresses<
|
export function expresses<
|
||||||
Traits extends readonly Trait<any>[],
|
Traits extends readonly Trait<any>[],
|
||||||
|
|||||||
@@ -3,10 +3,10 @@ import { expresses, trait } from "."
|
|||||||
|
|
||||||
const Identifiable = <ID>() =>
|
const Identifiable = <ID>() =>
|
||||||
trait(Parent => {
|
trait(Parent => {
|
||||||
abstract class Identified extends Parent {
|
abstract class Identifiable extends Parent {
|
||||||
abstract id: ID
|
abstract readonly id: ID
|
||||||
|
|
||||||
equals(el: Identified) {
|
equals(el: Identifiable) {
|
||||||
return this.id === el.id
|
return this.id === el.id
|
||||||
}
|
}
|
||||||
|
|
||||||
@@ -16,7 +16,7 @@ const Identifiable = <ID>() =>
|
|||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
return Identified
|
return Identifiable
|
||||||
})
|
})
|
||||||
|
|
||||||
const ImplementsIdentifiable = <ID>(defaultID: ID) =>
|
const ImplementsIdentifiable = <ID>(defaultID: ID) =>
|
||||||
|
|||||||
Reference in New Issue
Block a user