0.1.11 #11
@@ -5,75 +5,60 @@ import { TraitExpression } from "./TraitExpression"
|
|||||||
import { Extendable, StaticMembers } from "./util"
|
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<
|
export class TraitExpressionBuilder<
|
||||||
Superclass extends AbstractClass<object>,
|
Superclass extends AbstractClass<object>,
|
||||||
const Traits extends Trait<any, any, any, any>[],
|
const Traits extends Trait<any, any, any, any>[],
|
||||||
> {
|
> {
|
||||||
|
declare ["constructor"]: typeof TraitExpressionBuilder
|
||||||
|
|
||||||
constructor(
|
constructor(
|
||||||
readonly expressionSuperclass: Superclass,
|
readonly expressionSuperclass: Superclass,
|
||||||
readonly expressionTraits: Traits,
|
readonly expressionTraits: Traits,
|
||||||
) {}
|
) {}
|
||||||
|
|
||||||
|
|
||||||
|
static spreadSupertraits<
|
||||||
|
const T extends Trait<
|
||||||
|
TraitExpression<
|
||||||
|
typeof TraitExpression.NullSuperclass,
|
||||||
|
Trait<any, any, any, any>[]
|
||||||
|
>,
|
||||||
|
any,
|
||||||
|
any,
|
||||||
|
any
|
||||||
|
>[]
|
||||||
|
>(
|
||||||
|
traits: T
|
||||||
|
) {
|
||||||
|
return traits.flatMap(trait => [
|
||||||
|
...trait.superExpression.traits,
|
||||||
|
trait,
|
||||||
|
]) as TraitExpressionBuilder.SpreadSupertraits<T>
|
||||||
|
}
|
||||||
|
|
||||||
|
static uniqTraits<
|
||||||
|
const T extends Trait<
|
||||||
|
TraitExpression<
|
||||||
|
typeof TraitExpression.NullSuperclass,
|
||||||
|
Trait<any, any, any, any>[]
|
||||||
|
>,
|
||||||
|
any,
|
||||||
|
any,
|
||||||
|
any
|
||||||
|
>[]
|
||||||
|
>(
|
||||||
|
traits: T
|
||||||
|
) {
|
||||||
|
return uniq(traits) as TraitExpressionBuilder.UniqTraits<T>
|
||||||
|
}
|
||||||
|
|
||||||
|
|
||||||
extends<
|
extends<
|
||||||
Super extends AbstractClass<object>
|
Super extends AbstractClass<object>
|
||||||
>(
|
>(
|
||||||
superclass: Super
|
superclass: Super
|
||||||
) {
|
) {
|
||||||
return new TraitExpressionBuilder(
|
return new this.constructor(
|
||||||
superclass,
|
superclass,
|
||||||
this.expressionTraits,
|
this.expressionTraits,
|
||||||
)
|
)
|
||||||
@@ -92,16 +77,13 @@ export class TraitExpressionBuilder<
|
|||||||
>(
|
>(
|
||||||
...traits: T
|
...traits: T
|
||||||
) {
|
) {
|
||||||
return new TraitExpressionBuilder(
|
return new this.constructor(
|
||||||
this.expressionSuperclass,
|
this.expressionSuperclass,
|
||||||
|
|
||||||
uniq([
|
this.constructor.uniqTraits([
|
||||||
...this.expressionTraits,
|
...this.expressionTraits,
|
||||||
...traits.flatMap(trait => [
|
...this.constructor.spreadSupertraits(traits),
|
||||||
...trait.superExpression.traits,
|
|
||||||
trait,
|
|
||||||
]),
|
]),
|
||||||
]) as TraitsUniq<[...Traits, ...SpreadSupertraits<T>]>,
|
|
||||||
)
|
)
|
||||||
}
|
}
|
||||||
|
|
||||||
@@ -118,24 +100,22 @@ export class TraitExpressionBuilder<
|
|||||||
>(
|
>(
|
||||||
...traits: T
|
...traits: T
|
||||||
) {
|
) {
|
||||||
return new TraitExpressionBuilder(
|
return new this.constructor(
|
||||||
this.expressionSuperclass,
|
this.expressionSuperclass,
|
||||||
|
|
||||||
uniq([
|
this.constructor.uniqTraits([
|
||||||
...traits.flatMap(trait => [
|
...this.constructor.spreadSupertraits(traits),
|
||||||
...trait.superExpression.traits,
|
|
||||||
trait,
|
|
||||||
]),
|
|
||||||
...this.expressionTraits,
|
...this.expressionTraits,
|
||||||
]) as TraitsUniq<[...SpreadSupertraits<T>, ...Traits]>,
|
]),
|
||||||
)
|
)
|
||||||
}
|
}
|
||||||
|
|
||||||
|
|
||||||
build() {
|
build() {
|
||||||
return new TraitExpression(
|
return new TraitExpression(
|
||||||
this.expressionSuperclass,
|
this.expressionSuperclass,
|
||||||
this.expressionTraits,
|
this.expressionTraits,
|
||||||
) as BuildTraitExpression<Superclass, Traits>
|
) as TraitExpressionBuilder.BuildTraitExpression<Superclass, Traits>
|
||||||
}
|
}
|
||||||
|
|
||||||
then<V>(fn: (expression: ReturnType<typeof this.build>) => V): V {
|
then<V>(fn: (expression: ReturnType<typeof this.build>) => V): V {
|
||||||
@@ -154,4 +134,59 @@ 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 UniqTraits<Traits> = (
|
||||||
|
Traits extends [
|
||||||
|
...infer Rest,
|
||||||
|
infer El extends Trait<any, any, any, any>,
|
||||||
|
]
|
||||||
|
? IsTraitInTupleFromRight<Rest, El> extends true
|
||||||
|
? UniqTraits<Rest>
|
||||||
|
: [...UniqTraits<Rest>, El]
|
||||||
|
: []
|
||||||
|
)
|
||||||
|
type IsTraitInTupleFromRight<Traits, T> = (
|
||||||
|
Traits extends [...infer Rest, infer El]
|
||||||
|
? IsEqual<El, T> extends true
|
||||||
|
? true
|
||||||
|
: IsTraitInTupleFromRight<Rest, T>
|
||||||
|
: false
|
||||||
|
)
|
||||||
|
|
||||||
|
export 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 const expression = new TraitExpressionBuilder(TraitExpression.NullSuperclass, [])
|
export const expression = new TraitExpressionBuilder(TraitExpression.NullSuperclass, [])
|
||||||
|
|||||||
@@ -64,6 +64,7 @@ const exp = expression
|
|||||||
PrintsHelloOnNew,
|
PrintsHelloOnNew,
|
||||||
Identifiable<bigint>(),
|
Identifiable<bigint>(),
|
||||||
// Identifiable<number>(),
|
// Identifiable<number>(),
|
||||||
|
// StatefulSubscription,
|
||||||
ActiveStatefulSubscription,
|
ActiveStatefulSubscription,
|
||||||
)
|
)
|
||||||
.build()
|
.build()
|
||||||
|
|||||||
Reference in New Issue
Block a user