Co-authored-by: Julien Valverdé <julien.valverde@mailo.com> Reviewed-on: https://git.jvalver.de/Thilawyn/traitify-ts/pulls/11
This commit was merged in pull request #11.
This commit is contained in:
@@ -1,6 +1,6 @@
|
||||
{
|
||||
"name": "@thilawyn/traitify-ts",
|
||||
"version": "0.1.10",
|
||||
"version": "0.1.11",
|
||||
"type": "module",
|
||||
"publishConfig": {
|
||||
"registry": "https://git.jvalver.de/api/packages/thilawyn/npm/"
|
||||
@@ -29,7 +29,7 @@
|
||||
},
|
||||
"dependencies": {
|
||||
"lodash-es": "^4.17.21",
|
||||
"type-fest": "^4.10.2"
|
||||
"type-fest": "^4.10.3"
|
||||
},
|
||||
"devDependencies": {
|
||||
"@rollup/plugin-node-resolve": "^15.2.3",
|
||||
|
||||
@@ -6,7 +6,7 @@ import { Extend, StaticMembers } from "./util"
|
||||
export class Trait<
|
||||
SuperExpression extends TraitExpression<
|
||||
typeof TraitExpression.NullSuperclass,
|
||||
Trait<any, any, any, any>[]
|
||||
readonly Trait<any, any, any, any>[]
|
||||
>,
|
||||
Abstract extends object,
|
||||
StaticAbstract extends object,
|
||||
|
||||
@@ -6,7 +6,91 @@ import { Extend, StaticMembers } from "./util"
|
||||
|
||||
declare const implSuperSymbol: unique symbol
|
||||
|
||||
type ImplSuper<This> = (
|
||||
|
||||
export class TraitBuilder<
|
||||
SuperExpression extends TraitExpression<
|
||||
typeof TraitExpression.NullSuperclass,
|
||||
readonly Trait<any, any, any, any>[]
|
||||
>,
|
||||
Abstract extends object,
|
||||
StaticAbstract extends object,
|
||||
ImplClass extends AbstractClass<object, []>,
|
||||
> {
|
||||
declare ["constructor"]: typeof TraitBuilder
|
||||
|
||||
constructor(
|
||||
readonly traitSuperExpression: SuperExpression,
|
||||
readonly traitAbstract: Abstract,
|
||||
readonly traitStaticAbstract: StaticAbstract,
|
||||
readonly traitApply: (Super: AbstractClass<object>) => ImplClass,
|
||||
) {}
|
||||
|
||||
|
||||
abstract<A extends Abstract>(
|
||||
_: (Super: AbstractClass<Abstract>) => AbstractClass<A, []>
|
||||
) {
|
||||
return new this.constructor(
|
||||
this.traitSuperExpression,
|
||||
{} as Simplify<A>,
|
||||
this.traitStaticAbstract,
|
||||
this.traitApply,
|
||||
)
|
||||
}
|
||||
|
||||
staticAbstract<A extends StaticAbstract>(
|
||||
_: (Super: AbstractClass<StaticAbstract>) => AbstractClass<A, []>
|
||||
) {
|
||||
return new this.constructor(
|
||||
this.traitSuperExpression,
|
||||
this.traitAbstract,
|
||||
{} as Simplify<A>,
|
||||
this.traitApply,
|
||||
)
|
||||
}
|
||||
|
||||
implement<
|
||||
ImplClassWithAbstract extends TraitBuilder.ImplSuper<typeof this> // TODO: find a way to set the constraint to concrete classes while keeping the Super arg as an abstract class
|
||||
>(
|
||||
apply: (Super: TraitBuilder.ImplSuper<typeof this>) => ImplClassWithAbstract
|
||||
) {
|
||||
return new this.constructor(
|
||||
this.traitSuperExpression,
|
||||
this.traitAbstract,
|
||||
this.traitStaticAbstract,
|
||||
|
||||
apply as unknown as (Super: AbstractClass<object>) => (
|
||||
AbstractClass<
|
||||
Simplify<
|
||||
Omit<
|
||||
InstanceType<ImplClassWithAbstract>,
|
||||
keyof Abstract
|
||||
>
|
||||
>
|
||||
> &
|
||||
|
||||
Simplify<
|
||||
Omit<
|
||||
StaticMembers<ImplClassWithAbstract>,
|
||||
keyof StaticAbstract | typeof implSuperSymbol
|
||||
>
|
||||
>
|
||||
),
|
||||
)
|
||||
}
|
||||
|
||||
|
||||
build() {
|
||||
return new Trait(
|
||||
this.traitSuperExpression,
|
||||
this.traitAbstract,
|
||||
this.traitStaticAbstract,
|
||||
this.traitApply,
|
||||
)
|
||||
}
|
||||
}
|
||||
|
||||
export namespace TraitBuilder {
|
||||
export type ImplSuper<This> = (
|
||||
This extends TraitBuilder<
|
||||
any,
|
||||
infer Abstract,
|
||||
@@ -32,87 +116,10 @@ type ImplSuper<This> = (
|
||||
{ readonly [implSuperSymbol]: true }
|
||||
)
|
||||
: never
|
||||
)
|
||||
|
||||
|
||||
export class TraitBuilder<
|
||||
SuperExpression extends TraitExpression<
|
||||
typeof TraitExpression.NullSuperclass,
|
||||
Trait<any, any, any, any>[]
|
||||
>,
|
||||
Abstract extends object,
|
||||
StaticAbstract extends object,
|
||||
ImplClass extends AbstractClass<object, []>,
|
||||
> {
|
||||
constructor(
|
||||
readonly traitSuperExpression: SuperExpression,
|
||||
readonly traitAbstract: Abstract,
|
||||
readonly traitStaticAbstract: StaticAbstract,
|
||||
readonly traitApply: (Super: AbstractClass<object>) => ImplClass,
|
||||
) {}
|
||||
|
||||
abstract<A extends Abstract>(
|
||||
_: (Super: AbstractClass<Abstract>) => AbstractClass<A, []>
|
||||
) {
|
||||
return new TraitBuilder(
|
||||
this.traitSuperExpression,
|
||||
{} as Simplify<A>,
|
||||
this.traitStaticAbstract,
|
||||
this.traitApply,
|
||||
)
|
||||
}
|
||||
|
||||
staticAbstract<A extends StaticAbstract>(
|
||||
_: (Super: AbstractClass<StaticAbstract>) => AbstractClass<A, []>
|
||||
) {
|
||||
return new TraitBuilder(
|
||||
this.traitSuperExpression,
|
||||
this.traitAbstract,
|
||||
{} as Simplify<A>,
|
||||
this.traitApply,
|
||||
)
|
||||
}
|
||||
|
||||
implement<
|
||||
ImplClassWithAbstract extends ImplSuper<typeof this> // TODO: find a way to set the constraint to concrete classes while keeping the Super arg as an abstract class
|
||||
>(
|
||||
apply: (Super: ImplSuper<typeof this>) => ImplClassWithAbstract
|
||||
) {
|
||||
return new TraitBuilder(
|
||||
this.traitSuperExpression,
|
||||
this.traitAbstract,
|
||||
this.traitStaticAbstract,
|
||||
|
||||
apply as unknown as (Super: AbstractClass<object>) => (
|
||||
AbstractClass<
|
||||
Simplify<
|
||||
Omit<
|
||||
InstanceType<ImplClassWithAbstract>,
|
||||
keyof Abstract
|
||||
>
|
||||
>
|
||||
> &
|
||||
|
||||
Simplify<
|
||||
Omit<
|
||||
StaticMembers<ImplClassWithAbstract>,
|
||||
keyof StaticAbstract | typeof implSuperSymbol
|
||||
>
|
||||
>
|
||||
),
|
||||
)
|
||||
}
|
||||
|
||||
build() {
|
||||
return new Trait(
|
||||
this.traitSuperExpression,
|
||||
this.traitAbstract,
|
||||
this.traitStaticAbstract,
|
||||
this.traitApply,
|
||||
)
|
||||
}
|
||||
}
|
||||
|
||||
|
||||
export const trait = new TraitBuilder(
|
||||
new TraitExpression(TraitExpression.NullSuperclass, []),
|
||||
{} as object,
|
||||
|
||||
@@ -6,7 +6,7 @@ import { Extend, StaticMembers } from "./util"
|
||||
|
||||
export class TraitExpression<
|
||||
Superclass extends AbstractClass<object>,
|
||||
const Traits extends Trait<any, any, any, any>[],
|
||||
const Traits extends readonly Trait<any, any, any, any>[],
|
||||
> {
|
||||
constructor(
|
||||
readonly superclass: Superclass,
|
||||
@@ -95,7 +95,7 @@ export namespace TraitExpression {
|
||||
|
||||
|
||||
export type Implements<
|
||||
Exp extends TraitExpression<any, Trait<any, any, any, any>[]>
|
||||
Exp extends TraitExpression<any, readonly Trait<any, any, any, any>[]>
|
||||
> = (
|
||||
Simplify<
|
||||
Extend<
|
||||
@@ -107,7 +107,7 @@ export type Implements<
|
||||
)
|
||||
|
||||
export type StaticImplements<
|
||||
Exp extends TraitExpression<any, Trait<any, any, any, any>[]>
|
||||
Exp extends TraitExpression<any, readonly Trait<any, any, any, any>[]>
|
||||
> = (
|
||||
Simplify<
|
||||
Extend<
|
||||
@@ -120,7 +120,7 @@ export type StaticImplements<
|
||||
|
||||
|
||||
export type TraitExpressionClass<
|
||||
Exp extends TraitExpression<any, Trait<any, any, any, any>[]>
|
||||
Exp extends TraitExpression<any, readonly Trait<any, any, any, any>[]>
|
||||
> = (
|
||||
AbstractClass<
|
||||
TraitExpressionInstance<Exp>,
|
||||
@@ -130,7 +130,7 @@ export type TraitExpressionClass<
|
||||
)
|
||||
|
||||
export type TraitExpressionConcreteClass<
|
||||
Exp extends TraitExpression<any, Trait<any, any, any, any>[]>
|
||||
Exp extends TraitExpression<any, readonly Trait<any, any, any, any>[]>
|
||||
> = (
|
||||
Class<
|
||||
TraitExpressionInstance<Exp>,
|
||||
@@ -140,7 +140,7 @@ export type TraitExpressionConcreteClass<
|
||||
)
|
||||
|
||||
export type TraitExpressionInstance<
|
||||
Exp extends TraitExpression<any, Trait<any, any, any, any>[]>
|
||||
Exp extends TraitExpression<any, readonly Trait<any, any, any, any>[]>
|
||||
> = (
|
||||
InstanceType<TraitExpression.Superclass<Exp>> & // Keep the instance of the superclass outside of any kind of type manipulation
|
||||
// as it can accidentely remove abstract properties
|
||||
@@ -152,7 +152,7 @@ export type TraitExpressionInstance<
|
||||
)
|
||||
|
||||
export type TraitExpressionStaticMembers<
|
||||
Exp extends TraitExpression<any, Trait<any, any, any, any>[]>
|
||||
Exp extends TraitExpression<any, readonly Trait<any, any, any, any>[]>
|
||||
> = (
|
||||
Simplify<
|
||||
Extend<[
|
||||
|
||||
@@ -5,85 +5,70 @@ import { TraitExpression } from "./TraitExpression"
|
||||
import { Extendable, StaticMembers } from "./util"
|
||||
|
||||
|
||||
type SpreadSupertraits<Traits> = (
|
||||
Traits extends [
|
||||
infer El extends Trait<any, any, any, any>,
|
||||
...infer Rest,
|
||||
]
|
||||
? [
|
||||
...Trait.Supertraits<El>,
|
||||
El,
|
||||
...SpreadSupertraits<Rest>,
|
||||
]
|
||||
: []
|
||||
)
|
||||
|
||||
type TraitsUniq<Traits> = (
|
||||
Traits extends [
|
||||
...infer Rest,
|
||||
infer El extends Trait<any, any, any, any>,
|
||||
]
|
||||
? IsTraitInTupleFromRight<Rest, El> extends true
|
||||
? TraitsUniq<Rest>
|
||||
: [...TraitsUniq<Rest>, El]
|
||||
: []
|
||||
)
|
||||
type IsTraitInTupleFromRight<Traits, T> = (
|
||||
Traits extends [...infer Rest, infer El]
|
||||
? IsEqual<El, T> extends true
|
||||
? true
|
||||
: IsTraitInTupleFromRight<Rest, T>
|
||||
: false
|
||||
)
|
||||
|
||||
|
||||
type BuildTraitExpression<
|
||||
Superclass extends AbstractClass<object>,
|
||||
Traits extends Trait<any, any, any, any>[],
|
||||
> = (
|
||||
Extendable<TraitTuple.MapAbstract<Traits>> extends false
|
||||
? "Type conflict between the traits abstract definitions."
|
||||
: Extendable<TraitTuple.MapStaticAbstract<Traits>> extends false
|
||||
? "Type conflict between the traits static abstract definitions."
|
||||
: Extendable<[
|
||||
InstanceType<Superclass>,
|
||||
...TraitTuple.MapImplInstance<Traits>,
|
||||
]> extends false
|
||||
? "Type conflict between the traits implementation instance and/or the superclass instance."
|
||||
: Extendable<[
|
||||
StaticMembers<Superclass>,
|
||||
...TraitTuple.MapImplStaticMembers<Traits>,
|
||||
]> extends false
|
||||
? "Type conflict between the traits implementation static members and/or the superclass static members."
|
||||
: TraitExpression<Superclass, Traits>
|
||||
)
|
||||
|
||||
|
||||
export class TraitExpressionBuilder<
|
||||
Superclass extends AbstractClass<object>,
|
||||
const Traits extends Trait<any, any, any, any>[],
|
||||
const Traits extends readonly Trait<any, any, any, any>[],
|
||||
> {
|
||||
declare ["constructor"]: typeof TraitExpressionBuilder
|
||||
|
||||
constructor(
|
||||
readonly expressionSuperclass: Superclass,
|
||||
readonly expressionTraits: Traits,
|
||||
) {}
|
||||
|
||||
|
||||
static spreadSupertraits<
|
||||
const T extends readonly Trait<
|
||||
TraitExpression<
|
||||
typeof TraitExpression.NullSuperclass,
|
||||
readonly Trait<any, any, any, any>[]
|
||||
>,
|
||||
any,
|
||||
any,
|
||||
any
|
||||
>[]
|
||||
>(
|
||||
traits: T
|
||||
) {
|
||||
return traits.flatMap(trait => [
|
||||
...trait.superExpression.traits,
|
||||
trait,
|
||||
]) as TraitExpressionBuilder.SpreadSupertraits<T>
|
||||
}
|
||||
|
||||
static traitsUniq<
|
||||
const T extends readonly Trait<
|
||||
TraitExpression<
|
||||
typeof TraitExpression.NullSuperclass,
|
||||
readonly Trait<any, any, any, any>[]
|
||||
>,
|
||||
any,
|
||||
any,
|
||||
any
|
||||
>[]
|
||||
>(
|
||||
traits: T
|
||||
) {
|
||||
return uniq(traits) as TraitExpressionBuilder.TraitsUniq<T>
|
||||
}
|
||||
|
||||
|
||||
extends<
|
||||
Super extends AbstractClass<object>
|
||||
>(
|
||||
superclass: Super
|
||||
) {
|
||||
return new TraitExpressionBuilder(
|
||||
return new this.constructor(
|
||||
superclass,
|
||||
this.expressionTraits,
|
||||
)
|
||||
}
|
||||
|
||||
expresses<
|
||||
const T extends Trait<
|
||||
const T extends readonly Trait<
|
||||
TraitExpression<
|
||||
typeof TraitExpression.NullSuperclass,
|
||||
Trait<any, any, any, any>[]
|
||||
readonly Trait<any, any, any, any>[]
|
||||
>,
|
||||
any,
|
||||
any,
|
||||
@@ -91,25 +76,25 @@ export class TraitExpressionBuilder<
|
||||
>[]
|
||||
>(
|
||||
...traits: T
|
||||
) {
|
||||
return new TraitExpressionBuilder(
|
||||
): TraitExpressionBuilder<
|
||||
Superclass,
|
||||
TraitExpressionBuilder.ExpressesReturnTypeTraits<Traits, T>
|
||||
> {
|
||||
return new this.constructor(
|
||||
this.expressionSuperclass,
|
||||
|
||||
uniq([
|
||||
this.constructor.traitsUniq([
|
||||
...this.expressionTraits,
|
||||
...traits.flatMap(trait => [
|
||||
...trait.superExpression.traits,
|
||||
trait,
|
||||
...this.constructor.spreadSupertraits(traits),
|
||||
]),
|
||||
]) as TraitsUniq<[...Traits, ...SpreadSupertraits<T>]>,
|
||||
)
|
||||
}
|
||||
|
||||
expressesFirst<
|
||||
const T extends Trait<
|
||||
const T extends readonly Trait<
|
||||
TraitExpression<
|
||||
typeof TraitExpression.NullSuperclass,
|
||||
Trait<any, any, any, any>[]
|
||||
readonly Trait<any, any, any, any>[]
|
||||
>,
|
||||
any,
|
||||
any,
|
||||
@@ -117,25 +102,26 @@ export class TraitExpressionBuilder<
|
||||
>[]
|
||||
>(
|
||||
...traits: T
|
||||
) {
|
||||
return new TraitExpressionBuilder(
|
||||
): TraitExpressionBuilder<
|
||||
Superclass,
|
||||
TraitExpressionBuilder.ExpressesFirstReturnTypeTraits<Traits, T>
|
||||
> {
|
||||
return new this.constructor(
|
||||
this.expressionSuperclass,
|
||||
|
||||
uniq([
|
||||
...traits.flatMap(trait => [
|
||||
...trait.superExpression.traits,
|
||||
trait,
|
||||
]),
|
||||
this.constructor.traitsUniq([
|
||||
...this.constructor.spreadSupertraits(traits),
|
||||
...this.expressionTraits,
|
||||
]) as TraitsUniq<[...SpreadSupertraits<T>, ...Traits]>,
|
||||
]),
|
||||
)
|
||||
}
|
||||
|
||||
|
||||
build() {
|
||||
return new TraitExpression(
|
||||
this.expressionSuperclass,
|
||||
this.expressionTraits,
|
||||
) as BuildTraitExpression<Superclass, Traits>
|
||||
) as TraitExpressionBuilder.BuildTraitExpression<Superclass, Traits>
|
||||
}
|
||||
|
||||
then<V>(fn: (expression: ReturnType<typeof this.build>) => V): V {
|
||||
@@ -154,4 +140,79 @@ export class TraitExpressionBuilder<
|
||||
}
|
||||
}
|
||||
|
||||
export namespace TraitExpressionBuilder {
|
||||
export type SpreadSupertraits<Traits> = (
|
||||
Traits extends [
|
||||
infer El extends Trait<any, any, any, any>,
|
||||
...infer Rest,
|
||||
]
|
||||
? [
|
||||
...Trait.Supertraits<El>,
|
||||
El,
|
||||
...SpreadSupertraits<Rest>,
|
||||
]
|
||||
: []
|
||||
)
|
||||
|
||||
export type TraitsUniq<Traits> = (
|
||||
Traits extends [
|
||||
...infer Rest,
|
||||
infer El extends Trait<any, any, any, any>,
|
||||
]
|
||||
? IsTraitInTupleFromRight<Rest, El> extends true
|
||||
? TraitsUniq<Rest>
|
||||
: [...TraitsUniq<Rest>, El]
|
||||
: []
|
||||
)
|
||||
type IsTraitInTupleFromRight<Traits, T> = (
|
||||
Traits extends [...infer Rest, infer El]
|
||||
? IsEqual<El, T> extends true
|
||||
? true
|
||||
: IsTraitInTupleFromRight<Rest, T>
|
||||
: false
|
||||
)
|
||||
|
||||
export type ExpressesReturnTypeTraits<
|
||||
Traits extends readonly Trait<any, any, any, any>[],
|
||||
T extends readonly Trait<any, any, any, any>[],
|
||||
> = (
|
||||
TraitExpressionBuilder.TraitsUniq<[
|
||||
...Traits,
|
||||
...TraitExpressionBuilder.SpreadSupertraits<T>,
|
||||
]>
|
||||
)
|
||||
|
||||
export type ExpressesFirstReturnTypeTraits<
|
||||
Traits extends readonly Trait<any, any, any, any>[],
|
||||
T extends readonly Trait<any, any, any, any>[],
|
||||
> = (
|
||||
TraitExpressionBuilder.TraitsUniq<[
|
||||
...TraitExpressionBuilder.SpreadSupertraits<T>,
|
||||
...Traits,
|
||||
]>
|
||||
)
|
||||
|
||||
export type BuildTraitExpression<
|
||||
Superclass extends AbstractClass<object>,
|
||||
Traits extends readonly Trait<any, any, any, any>[],
|
||||
> = (
|
||||
Extendable<TraitTuple.MapAbstract<Traits>> extends false
|
||||
? "Type conflict between the traits abstract definitions."
|
||||
: Extendable<TraitTuple.MapStaticAbstract<Traits>> extends false
|
||||
? "Type conflict between the traits static abstract definitions."
|
||||
: Extendable<[
|
||||
InstanceType<Superclass>,
|
||||
...TraitTuple.MapImplInstance<Traits>,
|
||||
]> extends false
|
||||
? "Type conflict between the traits implementation instance and/or the superclass instance."
|
||||
: Extendable<[
|
||||
StaticMembers<Superclass>,
|
||||
...TraitTuple.MapImplStaticMembers<Traits>,
|
||||
]> extends false
|
||||
? "Type conflict between the traits implementation static members and/or the superclass static members."
|
||||
: TraitExpression<Superclass, Traits>
|
||||
)
|
||||
}
|
||||
|
||||
|
||||
export const expression = new TraitExpressionBuilder(TraitExpression.NullSuperclass, [])
|
||||
|
||||
@@ -64,6 +64,7 @@ const exp = expression
|
||||
PrintsHelloOnNew,
|
||||
Identifiable<bigint>(),
|
||||
// Identifiable<number>(),
|
||||
// StatefulSubscription,
|
||||
ActiveStatefulSubscription,
|
||||
)
|
||||
.build()
|
||||
|
||||
Reference in New Issue
Block a user