From 9391cbf4b3755f85bea50bb6b7aede77f4460fe8 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Julien=20Valverd=C3=A9?= Date: Sat, 30 Dec 2023 00:36:15 +0100 Subject: [PATCH] Added doc --- src/index.ts | 23 +++++++++++++++++++++++ src/tests.ts | 5 ++++- 2 files changed, 27 insertions(+), 1 deletion(-) diff --git a/src/index.ts b/src/index.ts index ea9832d..8351eca 100644 --- a/src/index.ts +++ b/src/index.ts @@ -53,6 +53,9 @@ export type UnwrapTraitC = * static readonly defaultPermissions: 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[]) { * super(...args) * } @@ -72,6 +75,7 @@ export type UnwrapTraitC = * return this.id === el.id * } * + * // Optional * constructor(...args: any[]) { * super(...args) * } @@ -80,6 +84,25 @@ export type UnwrapTraitC = * return Identifiable * }) * ``` + * Creates a subtrait: + * ```ts + * const ImplementsIdentifiable = (defaultID: ID) => + * trait(Parent => { + * abstract class ImplementsIdentifiable extends extendsAndExpresses( + * Parent, + * [Identifiable()], + * ) { + * id: ID = defaultID + * + * // Optional + * constructor(...args: any[]) { + * super(...args) + * } + * } + * + * return ImplementsIdentifiable + * }) + * ``` */ export function trait< C extends AbstractClass diff --git a/src/tests.ts b/src/tests.ts index 30e12c5..bb49d1f 100644 --- a/src/tests.ts +++ b/src/tests.ts @@ -21,7 +21,10 @@ const Identifiable = () => const ImplementsIdentifiable = (defaultID: ID) => trait(Parent => { - abstract class ImplementsIdentifiable extends extendsAndExpresses(Parent, [Identifiable()]) { + abstract class ImplementsIdentifiable extends extendsAndExpresses( + Parent, + [Identifiable()], + ) { id: ID = defaultID constructor(...args: any[]) {