20231230.0.0 (#6)
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:
Julien Valverdé
2023-12-30 00:40:56 +01:00
parent d0e574422a
commit 9a84c59731
5 changed files with 41 additions and 15 deletions

View File

@@ -23,7 +23,7 @@ local build_step = {
local pack_step = { local pack_step = {
name: "pack", name: "pack",
image: node_image, image: node_image,
commands: ["npm pack"], commands: ["npm pack --dry-run"],
}; };
local publish_step = { local publish_step = {

View File

@@ -1,10 +0,0 @@
/node_modules/
/src/
/.drone.jsonnet
/.gitignore
/.npmignore
/bun.lockb
/README.md
/rollup.config.js
/tsconfig.json
/tsconfig.tsbuildinfo

View File

@@ -1,10 +1,13 @@
{ {
"name": "@thilawyn/thilatrait", "name": "@thilawyn/thilatrait",
"version": "20231229.0.0", "version": "20231230.0.0",
"type": "module", "type": "module",
"publishConfig": { "publishConfig": {
"registry": "https://git.jvalver.de/api/packages/thilawyn/npm/" "registry": "https://git.jvalver.de/api/packages/thilawyn/npm/"
}, },
"files": [
"./dist"
],
"exports": { "exports": {
".": { ".": {
"import": { "import": {

View File

@@ -5,7 +5,9 @@ import { AbstractClass, AbstractConstructor, Opaque, UnionToIntersection } from
* Represents the static members of a class. * Represents the static members of a class.
* @template C - The class type. * @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[] = [] * 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)
* } * }
@@ -70,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)
* } * }
@@ -78,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

@@ -1,4 +1,4 @@
import { expresses, trait } from "." import { expresses, extendsAndExpresses, trait } from "."
const Identifiable = <ID>() => const Identifiable = <ID>() =>
@@ -21,8 +21,16 @@ const Identifiable = <ID>() =>
const ImplementsIdentifiable = <ID>(defaultID: ID) => const ImplementsIdentifiable = <ID>(defaultID: ID) =>
trait(Parent => { trait(Parent => {
abstract class ImplementsIdentifiable extends Identifiable<ID>()(Parent) { abstract class ImplementsIdentifiable extends extendsAndExpresses(
Parent,
[Identifiable<ID>()],
) {
id: ID = defaultID id: ID = defaultID
constructor(...args: any[]) {
super(...args)
console.log("ImplementsIdentifiable constructor")
}
} }
return ImplementsIdentifiable return ImplementsIdentifiable