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

This commit is contained in:
Julien Valverdé
2023-12-30 00:36:15 +01:00
parent cabf2e0d74
commit 9391cbf4b3
2 changed files with 27 additions and 1 deletions

View File

@@ -53,6 +53,9 @@ export type UnwrapTraitC<T> =
* static readonly defaultPermissions: string[] = [] * static readonly defaultPermissions: string[] = []
* permissions: string[] = [] * permissions: string[] = []
* *
* // Constructor is optional
* // If you wish to use it, make sure it takes any[] as an args array and passes it to the super call. This is necessary for inheritance to work properly.
* // Trait constructors cannot have typed arguments of their own, they only serve to run logic during object instantiation.
* constructor(...args: any[]) { * constructor(...args: any[]) {
* super(...args) * super(...args)
* } * }
@@ -72,6 +75,7 @@ export type UnwrapTraitC<T> =
* return this.id === el.id * return this.id === el.id
* } * }
* *
* // Optional
* constructor(...args: any[]) { * constructor(...args: any[]) {
* super(...args) * super(...args)
* } * }
@@ -80,6 +84,25 @@ export type UnwrapTraitC<T> =
* return Identifiable * return Identifiable
* }) * })
* ``` * ```
* Creates a subtrait:
* ```ts
* const ImplementsIdentifiable = <ID>(defaultID: ID) =>
* trait(Parent => {
* abstract class ImplementsIdentifiable extends extendsAndExpresses(
* Parent,
* [Identifiable<ID>()],
* ) {
* id: ID = defaultID
*
* // Optional
* constructor(...args: any[]) {
* super(...args)
* }
* }
*
* return ImplementsIdentifiable
* })
* ```
*/ */
export function trait< export function trait<
C extends AbstractClass<any> C extends AbstractClass<any>

View File

@@ -21,7 +21,10 @@ const Identifiable = <ID>() =>
const ImplementsIdentifiable = <ID>(defaultID: ID) => const ImplementsIdentifiable = <ID>(defaultID: ID) =>
trait(Parent => { trait(Parent => {
abstract class ImplementsIdentifiable extends extendsAndExpresses(Parent, [Identifiable<ID>()]) { abstract class ImplementsIdentifiable extends extendsAndExpresses(
Parent,
[Identifiable<ID>()],
) {
id: ID = defaultID id: ID = defaultID
constructor(...args: any[]) { constructor(...args: any[]) {