diff --git a/bun.lockb b/bun.lockb index f44c720..77b93b1 100755 Binary files a/bun.lockb and b/bun.lockb differ diff --git a/package.json b/package.json index ce1af94..6cd1b12 100644 --- a/package.json +++ b/package.json @@ -1,6 +1,6 @@ { "name": "@thilawyn/traitify-ts", - "version": "0.1.15", + "version": "0.1.16", "type": "module", "publishConfig": { "registry": "https://git.jvalver.de/api/packages/thilawyn/npm/" @@ -29,18 +29,18 @@ }, "dependencies": { "lodash-es": "^4.17.21", - "type-fest": "^4.12.0" + "type-fest": "^4.13.1" }, "devDependencies": { "@rollup/plugin-node-resolve": "^15.2.3", "@types/lodash-es": "^4.17.12", "bun-types": "latest", - "npm-check-updates": "^16.14.15", + "npm-check-updates": "^16.14.17", "npm-sort": "^0.0.4", - "rollup": "^4.12.1", + "rollup": "^4.13.0", "rollup-plugin-cleanup": "^3.2.1", "rollup-plugin-ts": "^3.4.5", "tsx": "^4.7.1", - "typescript": "^5.4.2" + "typescript": "^5.4.3" } } diff --git a/src/TraitBuilder.ts b/src/TraitBuilder.ts index a01f933..76a7b60 100644 --- a/src/TraitBuilder.ts +++ b/src/TraitBuilder.ts @@ -1,4 +1,4 @@ -import { AbstractClass, Simplify } from "type-fest" +import { AbstractClass, Class, Simplify } from "type-fest" import { Trait } from "./Trait" import { TraitExpression } from "./TraitExpression" import { Extend, StaticMembers } from "./util" @@ -128,6 +128,40 @@ export type ImplStatic< > ) +export function implStaticThis< + ImplClass extends AbstractClass & { readonly [implSuperSymbol]: true }, + This extends AbstractClass, +>( + _Impl: ImplClass, + thisArg: This, +) { + return thisArg as unknown as ( + AbstractClass< + InstanceType & InstanceType, + ConstructorParameters + > & + ImplStatic & + StaticMembers + ) +} + +export function implStaticInstantiableThis< + ImplClass extends AbstractClass & { readonly [implSuperSymbol]: true }, + This extends Class, +>( + _Impl: ImplClass, + thisArg: This, +) { + return thisArg as unknown as ( + Class< + InstanceType & InstanceType, + ConstructorParameters + > & + ImplStatic & + StaticMembers + ) +} + export const trait = new TraitBuilder( new TraitExpression(TraitExpression.NullSuperclass, []), diff --git a/src/lib.ts b/src/lib.ts index 3e9f23b..a96aa6a 100644 --- a/src/lib.ts +++ b/src/lib.ts @@ -1,4 +1,4 @@ export { Trait, TraitClass, TraitConcreteClass, TraitInstance, TraitStaticMembers, TraitTuple } from "./Trait" -export { ImplStatic, TraitBuilder, trait } from "./TraitBuilder" +export { ImplStatic, TraitBuilder, implStaticInstantiableThis, implStaticThis, trait } from "./TraitBuilder" export { Implements, StaticImplements, TraitExpression, TraitExpressionClass, TraitExpressionConcreteClass, TraitExpressionInstance, TraitExpressionStaticMembers } from "./TraitExpression" export { TraitExpressionBuilder, expression } from "./TraitExpressionBuilder" diff --git a/src/tests.ts b/src/tests.ts index af9b93f..7d7278c 100644 --- a/src/tests.ts +++ b/src/tests.ts @@ -28,6 +28,15 @@ const Identifiable = () => trait }) .build() +const ImplementsIdentifiable = (defaultID: ID) => expression + .expresses(Identifiable()) + .build() + .subtrait() + .implement(Super => class ImplementsIdentifiable extends Super { + readonly id = defaultID + }) + .build() + const StatefulSubscription = trait .abstract(Super => class extends Super { declare readonly isStatefulSubscription: true