This commit is contained in:
26
src/index.ts
26
src/index.ts
@@ -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,10 +155,8 @@ export function extendsAndExpresses<
|
|||||||
AbstractClass<
|
AbstractClass<
|
||||||
InstanceType<C> &
|
InstanceType<C> &
|
||||||
UnionToIntersection<
|
UnionToIntersection<
|
||||||
InstanceType<
|
TraitInstance<
|
||||||
UnwrapTraitC<
|
Traits[number]
|
||||||
Traits[number]
|
|
||||||
>
|
|
||||||
>
|
>
|
||||||
>,
|
>,
|
||||||
|
|
||||||
@@ -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)
|
||||||
}
|
}
|
||||||
|
|||||||
@@ -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
|
||||||
|
|
||||||
|
|||||||
Reference in New Issue
Block a user