0.1.1 #8

Merged
Thilawyn merged 6 commits from next into master 2024-01-07 22:49:30 +01:00
2 changed files with 17 additions and 11 deletions
Showing only changes of commit 487c2b7255 - Show all commits

View File

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

View File

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