Various API changes
All checks were successful
continuous-integration/drone/push Build is passing

This commit is contained in:
Julien Valverdé
2024-01-07 05:31:15 +01:00
parent 1630d4d736
commit 487c2b7255
2 changed files with 17 additions and 11 deletions

View File

@@ -34,11 +34,16 @@ export type TraitApplier<
* Unwraps the type of the class from a given trait. * Unwraps the type of the class from a given trait.
* @template T - The trait type. * @template T - The trait type.
*/ */
export type UnwrapTraitC<T> = export type TraitClass<T> =
T extends Trait<infer C> T extends Trait<infer C>
? C ? C
: never : never
export type TraitInstance<T> =
T extends Trait<infer C>
? InstanceType<C>
: never
/** /**
* Creates a trait using the provided trait applier function. * Creates a trait using the provided trait applier function.
@@ -90,7 +95,7 @@ export type UnwrapTraitC<T> =
* trait(Parent => { * trait(Parent => {
* abstract class ImplementsIdentifiable extends extendsAndExpresses( * abstract class ImplementsIdentifiable extends extendsAndExpresses(
* Parent, * Parent,
* [Identifiable<ID>()], * Identifiable<ID>(),
* ) { * ) {
* id: ID = defaultID * id: ID = defaultID
* *
@@ -123,7 +128,10 @@ export function trait<
* @example * @example
* Extends a superclass and applies traits: * Extends a superclass and applies traits:
* ```ts * ```ts
* class User extends extendsAndExpresses(Entity, [Identifiable<bigint>(), Permissible]) { * class User extends extendsAndExpresses(Entity,
* Identifiable<bigint>(),
* Permissible,
* ) {
* readonly id: bigint * readonly id: bigint
* *
* constructor(id: bigint) { * constructor(id: bigint) {
@@ -138,7 +146,7 @@ export function extendsAndExpresses<
Traits extends readonly Trait<any>[], Traits extends readonly Trait<any>[],
>( >(
extend: C, extend: C,
traits: Traits, ...traits: Traits
) { ) {
return traits.reduce( return traits.reduce(
(previous, trait) => trait(previous), (previous, trait) => trait(previous),
@@ -147,11 +155,9 @@ export function extendsAndExpresses<
AbstractClass< AbstractClass<
InstanceType<C> & InstanceType<C> &
UnionToIntersection< UnionToIntersection<
InstanceType< TraitInstance<
UnwrapTraitC<
Traits[number] Traits[number]
> >
>
>, >,
ConstructorParameters<C> ConstructorParameters<C>
@@ -160,7 +166,7 @@ export function extendsAndExpresses<
StaticMembers<C> & StaticMembers<C> &
StaticMembers< StaticMembers<
UnionToIntersection< UnionToIntersection<
UnwrapTraitC< TraitClass<
Traits[number] Traits[number]
> >
> >
@@ -191,5 +197,5 @@ export function expresses<
>( >(
...traits: Traits ...traits: Traits
) { ) {
return extendsAndExpresses(Object, traits) return extendsAndExpresses(Object, ...traits)
} }

View File

@@ -23,7 +23,7 @@ const ImplementsIdentifiable = <ID>(defaultID: ID) =>
trait(Parent => { trait(Parent => {
abstract class ImplementsIdentifiable extends extendsAndExpresses( abstract class ImplementsIdentifiable extends extendsAndExpresses(
Parent, Parent,
[Identifiable<ID>()], Identifiable<ID>(),
) { ) {
id: ID = defaultID id: ID = defaultID