Fixed TraitExpressionBuilder
All checks were successful
continuous-integration/drone/push Build is passing
All checks were successful
continuous-integration/drone/push Build is passing
This commit is contained in:
48
src/Trait.ts
48
src/Trait.ts
@@ -1,6 +1,6 @@
|
|||||||
import { AbstractClass, Class, Simplify } from "type-fest"
|
import { AbstractClass, Class, Simplify } from "type-fest"
|
||||||
import { TraitExpression } from "./TraitExpression"
|
import { TraitExpression } from "./TraitExpression"
|
||||||
import { Extend, StaticMembers } from "./util"
|
import { Extend, StaticMembers as StaticMembersUtil } from "./util"
|
||||||
|
|
||||||
|
|
||||||
export class Trait<
|
export class Trait<
|
||||||
@@ -27,6 +27,10 @@ export namespace Trait {
|
|||||||
: never
|
: never
|
||||||
)
|
)
|
||||||
|
|
||||||
|
export type Supertraits<T> = (
|
||||||
|
TraitExpression.Traits<Trait.SuperExpression<T>>
|
||||||
|
)
|
||||||
|
|
||||||
export type Abstract<T> = (
|
export type Abstract<T> = (
|
||||||
T extends Trait<any, infer Abstract, any, any>
|
T extends Trait<any, infer Abstract, any, any>
|
||||||
? Abstract
|
? Abstract
|
||||||
@@ -50,11 +54,21 @@ export namespace Trait {
|
|||||||
)
|
)
|
||||||
|
|
||||||
export type ImplStaticMembers<T> = (
|
export type ImplStaticMembers<T> = (
|
||||||
StaticMembers<Trait.ImplClass<T>>
|
StaticMembersUtil<Trait.ImplClass<T>>
|
||||||
)
|
)
|
||||||
|
|
||||||
export type Supertraits<T> = (
|
export type Instance<T> = (
|
||||||
TraitExpression.Traits<Trait.SuperExpression<T>>
|
Extend<[
|
||||||
|
Trait.Abstract<T>,
|
||||||
|
Trait.ImplInstance<T>,
|
||||||
|
]>
|
||||||
|
)
|
||||||
|
|
||||||
|
export type StaticMembers<T> = (
|
||||||
|
Extend<[
|
||||||
|
Trait.StaticAbstract<T>,
|
||||||
|
Trait.ImplStaticMembers<T>,
|
||||||
|
]>
|
||||||
)
|
)
|
||||||
}
|
}
|
||||||
|
|
||||||
@@ -88,6 +102,18 @@ export namespace TraitTuple {
|
|||||||
? T[K]
|
? T[K]
|
||||||
: Trait.ImplStaticMembers<T[K]>
|
: Trait.ImplStaticMembers<T[K]>
|
||||||
}
|
}
|
||||||
|
|
||||||
|
export type MapInstance<T> = {
|
||||||
|
[K in keyof T]: K extends keyof []
|
||||||
|
? T[K]
|
||||||
|
: Trait.Instance<T[K]>
|
||||||
|
}
|
||||||
|
|
||||||
|
export type MapStaticMembers<T> = {
|
||||||
|
[K in keyof T]: K extends keyof []
|
||||||
|
? T[K]
|
||||||
|
: Trait.StaticMembers<T[K]>
|
||||||
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
|
|
||||||
@@ -102,19 +128,9 @@ export type TraitConcreteClass<T extends Trait<any, any, any, any>> = (
|
|||||||
)
|
)
|
||||||
|
|
||||||
export type TraitInstance<T extends Trait<any, any, any, any>> = (
|
export type TraitInstance<T extends Trait<any, any, any, any>> = (
|
||||||
Simplify<
|
Simplify<Trait.Instance<T>>
|
||||||
Extend<[
|
|
||||||
Trait.Abstract<T>,
|
|
||||||
Trait.ImplInstance<T>,
|
|
||||||
]>
|
|
||||||
>
|
|
||||||
)
|
)
|
||||||
|
|
||||||
export type TraitStaticMembers<T extends Trait<any, any, any, any>> = (
|
export type TraitStaticMembers<T extends Trait<any, any, any, any>> = (
|
||||||
Simplify<
|
Simplify<Trait.StaticMembers<T>>
|
||||||
Extend<[
|
|
||||||
Trait.StaticAbstract<T>,
|
|
||||||
Trait.ImplStaticMembers<T>,
|
|
||||||
]>
|
|
||||||
>
|
|
||||||
)
|
)
|
||||||
|
|||||||
@@ -1,5 +1,5 @@
|
|||||||
import { AbstractClass, Class, Simplify } from "type-fest"
|
import { AbstractClass, Class, Simplify } from "type-fest"
|
||||||
import { Trait, TraitInstance, TraitStaticMembers, TraitTuple } from "./Trait"
|
import { Trait, TraitTuple } from "./Trait"
|
||||||
import { TraitBuilder } from "./TraitBuilder"
|
import { TraitBuilder } from "./TraitBuilder"
|
||||||
import { Extend, StaticMembers } from "./util"
|
import { Extend, StaticMembers } from "./util"
|
||||||
|
|
||||||
@@ -143,17 +143,10 @@ export type TraitExpressionInstance<
|
|||||||
Simplify<
|
Simplify<
|
||||||
Extend<[
|
Extend<[
|
||||||
InstanceType<TraitExpression.Superclass<Exp>>,
|
InstanceType<TraitExpression.Superclass<Exp>>,
|
||||||
...MapTraitsInstance<TraitExpression.Traits<Exp>>,
|
...TraitTuple.MapInstance<TraitExpression.Traits<Exp>>,
|
||||||
]>
|
]>
|
||||||
>
|
>
|
||||||
)
|
)
|
||||||
type MapTraitsInstance<T> = {
|
|
||||||
[K in keyof T]: K extends keyof []
|
|
||||||
? T[K]
|
|
||||||
: T[K] extends Trait<any, any, any, any>
|
|
||||||
? TraitInstance<T[K]>
|
|
||||||
: never
|
|
||||||
}
|
|
||||||
|
|
||||||
export type TraitExpressionStaticMembers<
|
export type TraitExpressionStaticMembers<
|
||||||
Exp extends TraitExpression<any, Trait<any, any, any, any>[]>
|
Exp extends TraitExpression<any, Trait<any, any, any, any>[]>
|
||||||
@@ -161,14 +154,7 @@ export type TraitExpressionStaticMembers<
|
|||||||
Simplify<
|
Simplify<
|
||||||
Extend<[
|
Extend<[
|
||||||
StaticMembers<TraitExpression.Superclass<Exp>>,
|
StaticMembers<TraitExpression.Superclass<Exp>>,
|
||||||
...MapTraitsStaticMembers<TraitExpression.Traits<Exp>>,
|
...TraitTuple.MapStaticMembers<TraitExpression.Traits<Exp>>,
|
||||||
]>
|
]>
|
||||||
>
|
>
|
||||||
)
|
)
|
||||||
type MapTraitsStaticMembers<T> = {
|
|
||||||
[K in keyof T]: K extends keyof []
|
|
||||||
? T[K]
|
|
||||||
: T[K] extends Trait<any, any, any, any>
|
|
||||||
? TraitStaticMembers<T[K]>
|
|
||||||
: never
|
|
||||||
}
|
|
||||||
|
|||||||
@@ -1,9 +1,9 @@
|
|||||||
import { Call, Fn, Pipe, Tuples } from "hotscript"
|
import { Call, Fn, Tuples } from "hotscript"
|
||||||
import { uniq } from "lodash-es"
|
import { uniq } from "lodash-es"
|
||||||
import { AbstractClass } from "type-fest"
|
import { AbstractClass } from "type-fest"
|
||||||
import { Trait } from "./Trait"
|
import { Trait, TraitTuple } from "./Trait"
|
||||||
import { TraitExpression } from "./TraitExpression"
|
import { TraitExpression } from "./TraitExpression"
|
||||||
import { ExtendableFn, StaticMembersFn } from "./util"
|
import { Extendable, StaticMembers } from "./util"
|
||||||
|
|
||||||
|
|
||||||
type SpreadSupertraits<Traits extends Trait<any, any, any, any>[]> = (
|
type SpreadSupertraits<Traits extends Trait<any, any, any, any>[]> = (
|
||||||
@@ -17,49 +17,35 @@ interface PrependTraitSupertraitsFn extends Fn {
|
|||||||
}
|
}
|
||||||
|
|
||||||
|
|
||||||
type AbstractMembersExtendable<
|
type InstanceExtendable<
|
||||||
Superclass extends AbstractClass<{}>,
|
Superclass extends AbstractClass<object>,
|
||||||
Traits extends Trait<any, any, any, any>[],
|
Traits extends Trait<any, any, any, any>[],
|
||||||
> = (
|
> = (
|
||||||
Pipe<Traits, [
|
Extendable<[
|
||||||
Tuples.Map<Trait.OwnAbstractFn>,
|
InstanceType<Superclass>,
|
||||||
Tuples.Prepend<InstanceType<Superclass>>,
|
...TraitTuple.MapInstance<Traits>,
|
||||||
]>
|
]>
|
||||||
)
|
)
|
||||||
|
|
||||||
type ImplInstanceExtendable<
|
type StaticMembersExtendable<
|
||||||
Superclass extends AbstractClass<{}>,
|
Superclass extends AbstractClass<object>,
|
||||||
Traits extends Trait<any, any, any, any>[],
|
Traits extends Trait<any, any, any, any>[],
|
||||||
> = (
|
> = (
|
||||||
Pipe<Traits, [
|
Extendable<[
|
||||||
Tuples.Map<Trait.OwnImplInstanceFn>,
|
StaticMembers<Superclass>,
|
||||||
Tuples.Prepend<InstanceType<Superclass>>,
|
...TraitTuple.MapStaticMembers<Traits>,
|
||||||
]>
|
|
||||||
)
|
|
||||||
|
|
||||||
type ImplStaticMembersExtendable<
|
|
||||||
Superclass extends AbstractClass<{}>,
|
|
||||||
Traits extends Trait<any, any, any, any>[],
|
|
||||||
> = (
|
|
||||||
Pipe<Traits, [
|
|
||||||
Tuples.Map<Trait.OwnImplClassFn>,
|
|
||||||
Tuples.Prepend<Superclass>,
|
|
||||||
Tuples.Map<StaticMembersFn>,
|
|
||||||
ExtendableFn,
|
|
||||||
]>
|
]>
|
||||||
)
|
)
|
||||||
|
|
||||||
type BuildTraitExpression<
|
type BuildTraitExpression<
|
||||||
Superclass extends AbstractClass<{}>,
|
Superclass extends AbstractClass<object>,
|
||||||
Traits extends Trait<any, any, any, any>[],
|
Traits extends Trait<any, any, any, any>[],
|
||||||
> = (
|
> = (
|
||||||
AbstractMembersExtendable<Superclass, Traits> extends false
|
InstanceExtendable<Superclass, Traits> extends false
|
||||||
? "Type conflict between the traits abstract members and/or the superclass instance."
|
? "Type conflict on the instance side."
|
||||||
: ImplInstanceExtendable<Superclass, Traits> extends false
|
: StaticMembersExtendable<Superclass, Traits> extends false
|
||||||
? "Type conflict between the traits implementation instances and/or the superclass instance."
|
? "Type conflict on the static side."
|
||||||
: ImplStaticMembersExtendable<Superclass, Traits> extends false
|
: TraitExpression<Superclass, Traits>
|
||||||
? "Type conflict between the traits implementation static members and/or the superclass static members."
|
|
||||||
: TraitExpression<Superclass, Traits>
|
|
||||||
)
|
)
|
||||||
|
|
||||||
|
|
||||||
|
|||||||
@@ -1,3 +1,4 @@
|
|||||||
|
import { Simplify } from "type-fest"
|
||||||
import { TraitClass } from "./Trait"
|
import { TraitClass } from "./Trait"
|
||||||
import { trait } from "./TraitBuilder"
|
import { trait } from "./TraitBuilder"
|
||||||
import { Implements, ImplementsStatic, TraitExpressionClass } from "./TraitExpression"
|
import { Implements, ImplementsStatic, TraitExpressionClass } from "./TraitExpression"
|
||||||
@@ -85,6 +86,6 @@ console.log(new User())
|
|||||||
|
|
||||||
|
|
||||||
type T = NonExtendableKeys<[
|
type T = NonExtendableKeys<[
|
||||||
{ prout: string },
|
|
||||||
{ prout: "gneugneu" },
|
{ prout: "gneugneu" },
|
||||||
|
{ prout: string },
|
||||||
]>
|
]>
|
||||||
|
|||||||
@@ -58,10 +58,9 @@ export type NonExtendableKeys<T extends readonly object[]> = (
|
|||||||
infer Self extends object,
|
infer Self extends object,
|
||||||
...infer Rest extends object[],
|
...infer Rest extends object[],
|
||||||
]
|
]
|
||||||
? {
|
? {[K in keyof Super & keyof Self]: Self[K] extends Super[K]
|
||||||
[K in keyof Super & keyof Self]: Self[K] extends Super[K]
|
? never
|
||||||
? never
|
: K
|
||||||
: K
|
|
||||||
}[keyof Super & keyof Self]
|
}[keyof Super & keyof Self]
|
||||||
| NonExtendableKeys<[
|
| NonExtendableKeys<[
|
||||||
Super & Self,
|
Super & Self,
|
||||||
|
|||||||
Reference in New Issue
Block a user