20231230.0.0 (#6)
All checks were successful
continuous-integration/drone/push Build is passing
All checks were successful
continuous-integration/drone/push Build is passing
Fixes subtraiting Co-authored-by: Julien Valverdé <julien.valverde@mailo.com> Reviewed-on: https://git.jvalver.de/Thilawyn/thilatrait/pulls/6
This commit was merged in pull request #6.
This commit is contained in:
27
src/index.ts
27
src/index.ts
@@ -5,7 +5,9 @@ import { AbstractClass, AbstractConstructor, Opaque, UnionToIntersection } from
|
||||
* Represents the static members of a class.
|
||||
* @template C - The class type.
|
||||
*/
|
||||
export type StaticMembers<C> = Pick<C, keyof C>
|
||||
export type StaticMembers<C> = {
|
||||
[Key in keyof C as Key extends "prototype" ? never : Key]: C[Key]
|
||||
}
|
||||
|
||||
|
||||
/**
|
||||
@@ -51,6 +53,9 @@ export type UnwrapTraitC<T> =
|
||||
* 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)
|
||||
* }
|
||||
@@ -70,6 +75,7 @@ export type UnwrapTraitC<T> =
|
||||
* return this.id === el.id
|
||||
* }
|
||||
*
|
||||
* // Optional
|
||||
* constructor(...args: any[]) {
|
||||
* super(...args)
|
||||
* }
|
||||
@@ -78,6 +84,25 @@ export type UnwrapTraitC<T> =
|
||||
* 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<
|
||||
C extends AbstractClass<any>
|
||||
|
||||
12
src/tests.ts
12
src/tests.ts
@@ -1,4 +1,4 @@
|
||||
import { expresses, trait } from "."
|
||||
import { expresses, extendsAndExpresses, trait } from "."
|
||||
|
||||
|
||||
const Identifiable = <ID>() =>
|
||||
@@ -21,8 +21,16 @@ const Identifiable = <ID>() =>
|
||||
|
||||
const ImplementsIdentifiable = <ID>(defaultID: ID) =>
|
||||
trait(Parent => {
|
||||
abstract class ImplementsIdentifiable extends Identifiable<ID>()(Parent) {
|
||||
abstract class ImplementsIdentifiable extends extendsAndExpresses(
|
||||
Parent,
|
||||
[Identifiable<ID>()],
|
||||
) {
|
||||
id: ID = defaultID
|
||||
|
||||
constructor(...args: any[]) {
|
||||
super(...args)
|
||||
console.log("ImplementsIdentifiable constructor")
|
||||
}
|
||||
}
|
||||
|
||||
return ImplementsIdentifiable
|
||||
|
||||
Reference in New Issue
Block a user