Co-authored-by: Julien Valverdé <julien.valverde@mailo.com> Reviewed-on: https://git.jvalver.de/Thilawyn/traitify-ts/pulls/4
This commit was merged in pull request #4.
This commit is contained in:
11
package.json
11
package.json
@@ -1,6 +1,6 @@
|
|||||||
{
|
{
|
||||||
"name": "@thilawyn/traitify-ts",
|
"name": "@thilawyn/traitify-ts",
|
||||||
"version": "0.1.3",
|
"version": "0.1.4",
|
||||||
"type": "module",
|
"type": "module",
|
||||||
"publishConfig": {
|
"publishConfig": {
|
||||||
"registry": "https://git.jvalver.de/api/packages/thilawyn/npm/"
|
"registry": "https://git.jvalver.de/api/packages/thilawyn/npm/"
|
||||||
@@ -28,18 +28,19 @@
|
|||||||
"clean:node": "rm -rf node_modules"
|
"clean:node": "rm -rf node_modules"
|
||||||
},
|
},
|
||||||
"dependencies": {
|
"dependencies": {
|
||||||
"hotscript": "^1.0.13",
|
"lodash-es": "^4.17.21",
|
||||||
"type-fest": "^4.10.2"
|
"type-fest": "^4.10.2"
|
||||||
},
|
},
|
||||||
"devDependencies": {
|
"devDependencies": {
|
||||||
"@rollup/plugin-node-resolve": "^15.2.3",
|
"@rollup/plugin-node-resolve": "^15.2.3",
|
||||||
|
"@types/lodash-es": "^4.17.12",
|
||||||
"bun-types": "latest",
|
"bun-types": "latest",
|
||||||
"npm-check-updates": "^16.14.14",
|
"npm-check-updates": "^16.14.15",
|
||||||
"npm-sort": "^0.0.4",
|
"npm-sort": "^0.0.4",
|
||||||
"rollup": "^4.9.6",
|
"rollup": "^4.12.0",
|
||||||
"rollup-plugin-cleanup": "^3.2.1",
|
"rollup-plugin-cleanup": "^3.2.1",
|
||||||
"rollup-plugin-ts": "^3.4.5",
|
"rollup-plugin-ts": "^3.4.5",
|
||||||
"tsx": "^4.7.0",
|
"tsx": "^4.7.1",
|
||||||
"typescript": "^5.3.3"
|
"typescript": "^5.3.3"
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|||||||
231
src/Trait.ts
231
src/Trait.ts
@@ -1,157 +1,136 @@
|
|||||||
import { Fn, Pipe, Tuples } from "hotscript"
|
import { AbstractClass, Class, Simplify } from "type-fest"
|
||||||
import { AbstractClass, Class, Opaque } from "type-fest"
|
import { TraitExpression } from "./TraitExpression"
|
||||||
import { TraitExpression, emptyTraitExpression } from "./TraitExpression"
|
import { Extend, StaticMembers } from "./util"
|
||||||
import { AbstractTag } from "./abstract"
|
|
||||||
import { ExtendFn, SimplifyFn, StaticMembers, StaticMembersFn } from "./util"
|
|
||||||
|
|
||||||
|
|
||||||
export type TraitApplierSuperTag = "@thilawyn/traitify-ts/TraitApplierSuper"
|
|
||||||
|
|
||||||
|
|
||||||
export type AddAbstractToImplClass<
|
|
||||||
ImplClass extends Class<{}, []>,
|
|
||||||
Abstract extends {},
|
|
||||||
> = (
|
|
||||||
Class<
|
|
||||||
Abstract & InstanceType<ImplClass>,
|
|
||||||
ConstructorParameters<ImplClass>
|
|
||||||
> &
|
|
||||||
StaticMembers<ImplClass>
|
|
||||||
)
|
|
||||||
|
|
||||||
export type RemoveAbstractFromImplClass<
|
|
||||||
ImplClassWithAbstract extends Class<Abstract, []> & { _tag: TraitApplierSuperTag },
|
|
||||||
Abstract extends {},
|
|
||||||
> = (
|
|
||||||
Class<
|
|
||||||
Omit<InstanceType<ImplClassWithAbstract>, keyof Abstract>,
|
|
||||||
ConstructorParameters<ImplClassWithAbstract>
|
|
||||||
> &
|
|
||||||
Omit<StaticMembers<ImplClassWithAbstract>, "_tag">
|
|
||||||
)
|
|
||||||
|
|
||||||
|
|
||||||
export class Trait<
|
export class Trait<
|
||||||
Supertraits extends TraitExpression<typeof TraitExpression.NullSuperclass, Trait<any, any, any>[], Trait<any, any, any>[]>,
|
SuperExpression extends TraitExpression<
|
||||||
Abstract extends {},
|
typeof TraitExpression.NullSuperclass,
|
||||||
ImplClass extends Class<{}, []>,
|
Trait<any, any, any, any>[]
|
||||||
|
>,
|
||||||
|
Abstract extends object,
|
||||||
|
StaticAbstract extends object,
|
||||||
|
ImplClass extends AbstractClass<object, []>,
|
||||||
> {
|
> {
|
||||||
constructor(
|
constructor(
|
||||||
readonly supertraits: Supertraits,
|
readonly superExpression: SuperExpression,
|
||||||
readonly abstract: Abstract,
|
readonly abstract: Abstract,
|
||||||
readonly apply: (Super: AbstractClass<{}>) => ImplClass,
|
readonly staticAbstract: StaticAbstract,
|
||||||
|
readonly apply: (Super: AbstractClass<object>) => ImplClass,
|
||||||
) {}
|
) {}
|
||||||
}
|
}
|
||||||
|
|
||||||
export namespace Trait {
|
export namespace Trait {
|
||||||
export type OwnSupertraits<T> = (
|
export type SuperExpression<T> = (
|
||||||
T extends Trait<infer Supertraits, any, any>
|
T extends Trait<infer SuperExpression, any, any, any>
|
||||||
? Supertraits
|
? SuperExpression
|
||||||
: never
|
: never
|
||||||
)
|
)
|
||||||
export interface OwnSupertraitsFn extends Fn {
|
|
||||||
return: Trait.OwnSupertraits<this["arg0"]>
|
|
||||||
}
|
|
||||||
|
|
||||||
export type OwnAbstract<T> = (
|
export type Supertraits<T> = (
|
||||||
T extends Trait<any, infer Abstract, any>
|
TraitExpression.Traits<Trait.SuperExpression<T>>
|
||||||
|
)
|
||||||
|
|
||||||
|
export type Abstract<T> = (
|
||||||
|
T extends Trait<any, infer Abstract, any, any>
|
||||||
? Abstract
|
? Abstract
|
||||||
: never
|
: never
|
||||||
)
|
)
|
||||||
export interface OwnAbstractFn extends Fn {
|
|
||||||
return: Trait.OwnAbstract<this["arg0"]>
|
|
||||||
}
|
|
||||||
|
|
||||||
export type OwnImplClass<T> = (
|
export type StaticAbstract<T> = (
|
||||||
T extends Trait<any, any, infer ImplClass>
|
T extends Trait<any, any, infer StaticAbstract, any>
|
||||||
|
? StaticAbstract
|
||||||
|
: never
|
||||||
|
)
|
||||||
|
|
||||||
|
export type ImplClass<T> = (
|
||||||
|
T extends Trait<any, any, any, infer ImplClass>
|
||||||
? ImplClass
|
? ImplClass
|
||||||
: never
|
: never
|
||||||
)
|
)
|
||||||
export interface OwnImplClassFn extends Fn {
|
|
||||||
return: Trait.OwnImplClass<this["arg0"]>
|
|
||||||
}
|
|
||||||
|
|
||||||
export type OwnImplInstance<T> = (
|
export type ImplInstance<T> = (
|
||||||
T extends Trait<any, any, infer ImplClass>
|
InstanceType<Trait.ImplClass<T>>
|
||||||
? InstanceType<ImplClass>
|
|
||||||
: never
|
|
||||||
)
|
)
|
||||||
export interface OwnImplInstanceFn extends Fn {
|
|
||||||
return: Trait.OwnImplInstance<this["arg0"]>
|
|
||||||
}
|
|
||||||
|
|
||||||
export type OwnClass<T> = (
|
export type ImplStaticMembers<T> = (
|
||||||
T extends Trait<any, infer Abstract, infer ImplClass>
|
StaticMembers<Trait.ImplClass<T>>
|
||||||
? AddAbstractToImplClass<ImplClass, Abstract>
|
|
||||||
: never
|
|
||||||
)
|
)
|
||||||
export interface OwnClassFn extends Fn {
|
|
||||||
return: Trait.OwnClass<this["arg0"]>
|
|
||||||
}
|
|
||||||
|
|
||||||
export type OwnInstance<T> = (
|
|
||||||
T extends Trait<any, infer Abstract, infer ImplClass>
|
|
||||||
? InstanceType<
|
|
||||||
AddAbstractToImplClass<ImplClass, Abstract>
|
|
||||||
>
|
|
||||||
: never
|
|
||||||
)
|
|
||||||
export interface OwnInstanceFn extends Fn {
|
|
||||||
return: Trait.OwnInstance<this["arg0"]>
|
|
||||||
}
|
|
||||||
|
|
||||||
export type Supertraits<T> = (
|
|
||||||
T extends Trait<infer Supertraits, any, any>
|
|
||||||
? TraitExpression.AllTraits<Supertraits>
|
|
||||||
: never
|
|
||||||
)
|
|
||||||
export interface SupertraitsFn extends Fn {
|
|
||||||
return: Trait.Supertraits<this["arg0"]>
|
|
||||||
}
|
|
||||||
|
|
||||||
export type Class<T> = (
|
|
||||||
AbstractClass<
|
|
||||||
Trait.Instance<T>,
|
|
||||||
any[]
|
|
||||||
> &
|
|
||||||
Pipe<T, [
|
|
||||||
Trait.SupertraitsFn,
|
|
||||||
Tuples.Append<T>,
|
|
||||||
Tuples.Map<Trait.OwnClassFn>,
|
|
||||||
Tuples.Map<StaticMembersFn>,
|
|
||||||
ExtendFn,
|
|
||||||
SimplifyFn,
|
|
||||||
]>
|
|
||||||
)
|
|
||||||
export interface ClassFn extends Fn {
|
|
||||||
return: Trait.Class<this["arg0"]>
|
|
||||||
}
|
|
||||||
|
|
||||||
export type Instance<T> = (
|
export type Instance<T> = (
|
||||||
Pipe<T, [
|
Extend<[
|
||||||
Trait.SupertraitsFn,
|
Trait.Abstract<T>,
|
||||||
Tuples.Append<T>,
|
Trait.ImplInstance<T>,
|
||||||
Tuples.Map<Trait.OwnInstanceFn>,
|
|
||||||
ExtendFn,
|
|
||||||
SimplifyFn,
|
|
||||||
]>
|
]>
|
||||||
)
|
)
|
||||||
export interface InstanceFn extends Fn {
|
|
||||||
return: Trait.Instance<this["arg0"]>
|
|
||||||
}
|
|
||||||
}
|
|
||||||
|
|
||||||
|
export type Static<T> = (
|
||||||
export function trait<
|
Extend<[
|
||||||
Abstract extends {},
|
Trait.StaticAbstract<T>,
|
||||||
ImplClassWithAbstract extends Class<Abstract, []> & { _tag: TraitApplierSuperTag },
|
Trait.ImplStaticMembers<T>,
|
||||||
>(
|
]>
|
||||||
abstract: Opaque<Abstract, AbstractTag>,
|
|
||||||
apply: (Super: AbstractClass<Abstract> & { _tag: TraitApplierSuperTag }) => ImplClassWithAbstract,
|
|
||||||
) {
|
|
||||||
return new Trait(
|
|
||||||
emptyTraitExpression,
|
|
||||||
abstract as Abstract,
|
|
||||||
apply as any as (Super: AbstractClass<{}>) => RemoveAbstractFromImplClass<ImplClassWithAbstract, Abstract>,
|
|
||||||
)
|
)
|
||||||
}
|
}
|
||||||
|
|
||||||
|
export namespace TraitTuple {
|
||||||
|
export type MapAbstract<T> = {
|
||||||
|
[K in keyof T]: K extends keyof []
|
||||||
|
? T[K]
|
||||||
|
: Trait.Abstract<T[K]>
|
||||||
|
}
|
||||||
|
|
||||||
|
export type MapStaticAbstract<T> = {
|
||||||
|
[K in keyof T]: K extends keyof []
|
||||||
|
? T[K]
|
||||||
|
: Trait.StaticAbstract<T[K]>
|
||||||
|
}
|
||||||
|
|
||||||
|
export type MapImplClass<T> = {
|
||||||
|
[K in keyof T]: K extends keyof []
|
||||||
|
? T[K]
|
||||||
|
: Trait.ImplClass<T[K]>
|
||||||
|
}
|
||||||
|
|
||||||
|
export type MapImplInstance<T> = {
|
||||||
|
[K in keyof T]: K extends keyof []
|
||||||
|
? T[K]
|
||||||
|
: Trait.ImplInstance<T[K]>
|
||||||
|
}
|
||||||
|
|
||||||
|
export type MapImplStaticMembers<T> = {
|
||||||
|
[K in keyof T]: K extends keyof []
|
||||||
|
? 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.Static<T[K]>
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
|
||||||
|
export type TraitClass<T extends Trait<any, any, any, any>> = (
|
||||||
|
AbstractClass<TraitInstance<T>, any[]> &
|
||||||
|
TraitStaticMembers<T>
|
||||||
|
)
|
||||||
|
|
||||||
|
export type TraitConcreteClass<T extends Trait<any, any, any, any>> = (
|
||||||
|
Class<TraitInstance<T>, any[]> &
|
||||||
|
TraitStaticMembers<T>
|
||||||
|
)
|
||||||
|
|
||||||
|
export type TraitInstance<T extends Trait<any, any, any, any>> = (
|
||||||
|
Simplify<Trait.Instance<T>>
|
||||||
|
)
|
||||||
|
|
||||||
|
export type TraitStaticMembers<T extends Trait<any, any, any, any>> = (
|
||||||
|
Simplify<Trait.Static<T>>
|
||||||
|
)
|
||||||
|
|||||||
121
src/TraitBuilder.ts
Normal file
121
src/TraitBuilder.ts
Normal file
@@ -0,0 +1,121 @@
|
|||||||
|
import { AbstractClass, Simplify } from "type-fest"
|
||||||
|
import { Trait } from "./Trait"
|
||||||
|
import { TraitExpression } from "./TraitExpression"
|
||||||
|
import { Extend, StaticMembers } from "./util"
|
||||||
|
|
||||||
|
|
||||||
|
declare const implSuperSymbol: unique symbol
|
||||||
|
|
||||||
|
type ImplSuper<This> = (
|
||||||
|
This extends TraitBuilder<
|
||||||
|
any,
|
||||||
|
infer Abstract,
|
||||||
|
infer StaticAbstract,
|
||||||
|
infer ImplClass
|
||||||
|
>
|
||||||
|
? (
|
||||||
|
AbstractClass<
|
||||||
|
Simplify<
|
||||||
|
Extend<[
|
||||||
|
Abstract,
|
||||||
|
InstanceType<ImplClass>,
|
||||||
|
]>
|
||||||
|
>
|
||||||
|
> &
|
||||||
|
|
||||||
|
Simplify<
|
||||||
|
Extend<[
|
||||||
|
StaticAbstract,
|
||||||
|
StaticMembers<ImplClass>,
|
||||||
|
]>
|
||||||
|
> &
|
||||||
|
{ 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(
|
||||||
|
private readonly traitSuperExpression: SuperExpression,
|
||||||
|
private readonly traitAbstract: Abstract,
|
||||||
|
private readonly traitStaticAbstract: StaticAbstract,
|
||||||
|
private 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,
|
||||||
|
{} as object,
|
||||||
|
Super => class extends Super {} as AbstractClass<object>,
|
||||||
|
)
|
||||||
@@ -1,113 +1,163 @@
|
|||||||
import { Fn, Pipe, Tuples } from "hotscript"
|
import { AbstractClass, Class, Simplify } from "type-fest"
|
||||||
import { AbstractClass, Class, Opaque } from "type-fest"
|
import { Trait, TraitTuple } from "./Trait"
|
||||||
import { RemoveAbstractFromImplClass, Trait, TraitApplierSuperTag } from "./Trait"
|
import { TraitBuilder } from "./TraitBuilder"
|
||||||
import { AbstractTag } from "./abstract"
|
import { Extend, StaticMembers } from "./util"
|
||||||
import { ExtendFn, SimplifyFn, StaticMembersFn } from "./util"
|
|
||||||
|
|
||||||
|
|
||||||
// type RemoveSupertraitsAbstractFromAbstract<Left, Right> = {
|
|
||||||
// [Key in Extract<keyof Left, keyof Right>]: Left[Key]
|
|
||||||
// } & {
|
|
||||||
// [Key in Exclude<keyof Left, keyof Right>]: Left[Key]
|
|
||||||
// }
|
|
||||||
|
|
||||||
|
|
||||||
export class TraitExpression<
|
export class TraitExpression<
|
||||||
Superclass extends AbstractClass<{}>,
|
Superclass extends AbstractClass<object>,
|
||||||
const OwnTraits extends Trait<any, any, any>[],
|
const Traits extends Trait<any, any, any, any>[],
|
||||||
const AllTraits extends Trait<any, any, any>[],
|
|
||||||
> {
|
> {
|
||||||
constructor(
|
constructor(
|
||||||
readonly superclass: Superclass,
|
readonly superclass: Superclass,
|
||||||
readonly ownTraits: OwnTraits,
|
readonly traits: Traits,
|
||||||
readonly allTraits: AllTraits,
|
|
||||||
) {}
|
) {}
|
||||||
|
|
||||||
get extends(): (
|
get extends(): (
|
||||||
AbstractClass<
|
AbstractClass<
|
||||||
Pipe<AllTraits, [
|
InstanceType<Superclass> & // Keep the instance of the superclass outside of any kind of type manipulation
|
||||||
Tuples.Map<Trait.OwnImplInstanceFn>, // Map all the traits to the instance of their implementation class
|
// as it can accidentely remove abstract properties
|
||||||
Tuples.Prepend<InstanceType<Superclass>>, // Add the instance of the superclass at the top of the list
|
Simplify<
|
||||||
ExtendFn, // Reduce to a single instance that extends all the instances in the list
|
Extend<
|
||||||
SimplifyFn, // Make readable for IDEs
|
TraitTuple.MapImplInstance<Traits>
|
||||||
]>,
|
>
|
||||||
|
>,
|
||||||
|
|
||||||
ConstructorParameters<Superclass>
|
ConstructorParameters<Superclass>
|
||||||
> &
|
> &
|
||||||
|
|
||||||
Pipe<AllTraits, [
|
Simplify<
|
||||||
Tuples.Map<Trait.OwnImplClassFn>, // Map all the traits to their implementation class
|
Extend<[
|
||||||
Tuples.Prepend<Superclass>, // Add the superclass at the top of the list
|
StaticMembers<Superclass>,
|
||||||
Tuples.Map<StaticMembersFn>, // Map all the classes to an object containing their static members
|
...TraitTuple.MapImplStaticMembers<Traits>,
|
||||||
ExtendFn, // Reduce to a single object that extends all the objects in the list
|
|
||||||
SimplifyFn, // Make readable for IDEs
|
|
||||||
]>
|
]>
|
||||||
|
>
|
||||||
) {
|
) {
|
||||||
return this.allTraits.reduce(
|
return this.traits.reduce(
|
||||||
(previous, trait) => trait.apply(previous),
|
(previous, trait) => trait.apply(previous),
|
||||||
this.superclass,
|
this.superclass,
|
||||||
) as any
|
) as any
|
||||||
}
|
}
|
||||||
|
|
||||||
|
implementsStatic(target: ImplementsStatic<typeof this>, context: any) {}
|
||||||
|
|
||||||
subtrait<
|
subtrait<
|
||||||
This extends TraitExpression<typeof TraitExpression.NullSuperclass, any, any>,
|
This extends TraitExpression<typeof TraitExpression.NullSuperclass, any>
|
||||||
SubtraitAbstract extends Implements<This>,
|
|
||||||
SubtraitImplClassWithAbstract extends Class<SubtraitAbstract, []> & { _tag: TraitApplierSuperTag },
|
|
||||||
>(
|
>(
|
||||||
this: This,
|
this: This
|
||||||
abstract: (expression: This) => Opaque<SubtraitAbstract, AbstractTag>,
|
|
||||||
apply: (Super: AbstractClass<SubtraitAbstract> & { _tag: TraitApplierSuperTag }) => SubtraitImplClassWithAbstract,
|
|
||||||
) {
|
) {
|
||||||
return new Trait(
|
return new TraitBuilder<
|
||||||
|
This,
|
||||||
|
|
||||||
|
Simplify<
|
||||||
|
Extend<TraitTuple.MapAbstract<Traits>>
|
||||||
|
>,
|
||||||
|
Simplify<
|
||||||
|
Extend<TraitTuple.MapStaticAbstract<Traits>>
|
||||||
|
>,
|
||||||
|
|
||||||
|
AbstractClass<
|
||||||
|
Simplify<
|
||||||
|
Extend<TraitTuple.MapImplInstance<Traits>>
|
||||||
|
>
|
||||||
|
> &
|
||||||
|
Simplify<
|
||||||
|
Extend<TraitTuple.MapImplStaticMembers<Traits>>
|
||||||
|
>
|
||||||
|
>(
|
||||||
this,
|
this,
|
||||||
// {} as RemoveSupertraitsAbstractFromAbstract<SubtraitAbstract, Implements<typeof this>>,
|
{} as any,
|
||||||
{} as SubtraitAbstract, // TODO: find a way to cleanly substract Implements<typeof this> from this.
|
{} as any,
|
||||||
apply as any as (Super: AbstractClass<{}>) => RemoveAbstractFromImplClass<SubtraitImplClassWithAbstract, SubtraitAbstract>,
|
Super => class extends Super {} as any,
|
||||||
)
|
)
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
export namespace TraitExpression {
|
export namespace TraitExpression {
|
||||||
|
declare const nullSuperclassSymbol: unique symbol
|
||||||
export class NullSuperclass {
|
export class NullSuperclass {
|
||||||
static readonly _tag = "@thilawyn/traitify-ts/TraitExpression.NullSuperclass"
|
static readonly [nullSuperclassSymbol]: true
|
||||||
|
constructor(...args: any[]) {}
|
||||||
}
|
}
|
||||||
|
|
||||||
export type Superclass<T> = (
|
export type Superclass<T> = (
|
||||||
T extends TraitExpression<infer Superclass, any, any>
|
T extends TraitExpression<infer Superclass, any>
|
||||||
? Superclass
|
? Superclass
|
||||||
: never
|
: never
|
||||||
)
|
)
|
||||||
export interface SuperclassFn extends Fn {
|
|
||||||
return: TraitExpression.Superclass<this["arg0"]>
|
|
||||||
}
|
|
||||||
|
|
||||||
export type OwnTraits<T> = (
|
export type Traits<T> = (
|
||||||
T extends TraitExpression<any, infer OwnTraits, any>
|
T extends TraitExpression<any, infer Traits>
|
||||||
? OwnTraits
|
? Traits
|
||||||
: never
|
: never
|
||||||
)
|
)
|
||||||
export interface OwnTraitsFn extends Fn {
|
|
||||||
return: TraitExpression.OwnTraits<this["arg0"]>
|
|
||||||
}
|
}
|
||||||
|
|
||||||
export type AllTraits<T> = (
|
|
||||||
T extends TraitExpression<any, any, infer AllTraits>
|
export type Implements<
|
||||||
? AllTraits
|
Exp extends TraitExpression<any, Trait<any, any, any, any>[]>
|
||||||
: never
|
> = (
|
||||||
|
Simplify<
|
||||||
|
Extend<
|
||||||
|
TraitTuple.MapAbstract<
|
||||||
|
TraitExpression.Traits<Exp>
|
||||||
|
>
|
||||||
|
>
|
||||||
|
>
|
||||||
)
|
)
|
||||||
export interface AllTraitsFn extends Fn {
|
|
||||||
return: TraitExpression.AllTraits<this["arg0"]>
|
|
||||||
}
|
|
||||||
}
|
|
||||||
|
|
||||||
export const emptyTraitExpression = new TraitExpression(TraitExpression.NullSuperclass, [], [])
|
export type ImplementsStatic<
|
||||||
|
Exp extends TraitExpression<any, Trait<any, any, any, any>[]>
|
||||||
|
> = (
|
||||||
|
Simplify<
|
||||||
|
Extend<
|
||||||
|
TraitTuple.MapStaticAbstract<
|
||||||
|
TraitExpression.Traits<Exp>
|
||||||
|
>
|
||||||
|
>
|
||||||
|
>
|
||||||
|
)
|
||||||
|
|
||||||
|
|
||||||
export type Implements<Exp extends TraitExpression<any, any, any>> = (
|
export type TraitExpressionClass<
|
||||||
Pipe<Exp, [
|
Exp extends TraitExpression<any, Trait<any, any, any, any>[]>
|
||||||
TraitExpression.AllTraitsFn,
|
> = (
|
||||||
Tuples.Map<Trait.OwnAbstractFn>,
|
AbstractClass<
|
||||||
ExtendFn,
|
TraitExpressionInstance<Exp>,
|
||||||
SimplifyFn,
|
ConstructorParameters<TraitExpression.Superclass<Exp>>
|
||||||
|
> &
|
||||||
|
TraitExpressionStaticMembers<Exp>
|
||||||
|
)
|
||||||
|
|
||||||
|
export type TraitExpressionConcreteClass<
|
||||||
|
Exp extends TraitExpression<any, Trait<any, any, any, any>[]>
|
||||||
|
> = (
|
||||||
|
Class<
|
||||||
|
TraitExpressionInstance<Exp>,
|
||||||
|
ConstructorParameters<TraitExpression.Superclass<Exp>>
|
||||||
|
> &
|
||||||
|
TraitExpressionStaticMembers<Exp>
|
||||||
|
)
|
||||||
|
|
||||||
|
export type TraitExpressionInstance<
|
||||||
|
Exp extends TraitExpression<any, 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
|
||||||
|
Simplify<
|
||||||
|
Extend<
|
||||||
|
TraitTuple.MapInstance<TraitExpression.Traits<Exp>>
|
||||||
|
>
|
||||||
|
>
|
||||||
|
)
|
||||||
|
|
||||||
|
export type TraitExpressionStaticMembers<
|
||||||
|
Exp extends TraitExpression<any, Trait<any, any, any, any>[]>
|
||||||
|
> = (
|
||||||
|
Simplify<
|
||||||
|
Extend<[
|
||||||
|
StaticMembers<TraitExpression.Superclass<Exp>>,
|
||||||
|
...TraitTuple.MapStaticMembers<TraitExpression.Traits<Exp>>,
|
||||||
]>
|
]>
|
||||||
|
>
|
||||||
)
|
)
|
||||||
|
|||||||
@@ -1,129 +1,120 @@
|
|||||||
import { Call, Fn, Pipe, Tuples } from "hotscript"
|
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, emptyTraitExpression } from "./TraitExpression"
|
import { TraitExpression } from "./TraitExpression"
|
||||||
import { ExtendableFn, StaticMembersFn } from "./util"
|
import { Extendable, StaticMembers } from "./util"
|
||||||
|
|
||||||
|
|
||||||
type SpreadSupertraits<Traits extends Trait<any, any, any>[]> = (
|
type SpreadSupertraits<T> = (
|
||||||
Call<
|
T extends [infer Trait, ...infer Rest]
|
||||||
Tuples.FlatMap<PrependTraitSupertraitsFn>,
|
? [
|
||||||
Traits
|
...Trait.Supertraits<Trait>,
|
||||||
>
|
Trait,
|
||||||
|
...SpreadSupertraits<Rest>,
|
||||||
|
]
|
||||||
|
: []
|
||||||
)
|
)
|
||||||
interface PrependTraitSupertraitsFn extends Fn {
|
|
||||||
return: [...Trait.Supertraits<this["arg0"]>, this["arg0"]]
|
|
||||||
}
|
|
||||||
|
|
||||||
|
|
||||||
type AbstractMembersExtendable<
|
type InstanceExtendable<
|
||||||
Superclass extends AbstractClass<{}>,
|
Superclass extends AbstractClass<object>,
|
||||||
Traits extends Trait<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>[],
|
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>[],
|
|
||||||
> = (
|
|
||||||
Pipe<Traits, [
|
|
||||||
Tuples.Map<Trait.OwnImplClassFn>,
|
|
||||||
Tuples.Prepend<Superclass>,
|
|
||||||
Tuples.Map<StaticMembersFn>,
|
|
||||||
ExtendableFn,
|
|
||||||
]>
|
]>
|
||||||
)
|
)
|
||||||
|
|
||||||
type BuildTraitExpression<
|
type BuildTraitExpression<
|
||||||
Superclass extends AbstractClass<{}>,
|
Superclass extends AbstractClass<object>,
|
||||||
OwnTraits extends Trait<any, any, any>[],
|
Traits extends Trait<any, any, any, any>[],
|
||||||
AllTraits extends Trait<any, any, any>[],
|
|
||||||
> = (
|
> = (
|
||||||
AbstractMembersExtendable<Superclass, AllTraits> 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, AllTraits> 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, AllTraits> extends false
|
: TraitExpression<Superclass, Traits>
|
||||||
? "Type conflict between the traits implementation static members and/or the superclass static members."
|
|
||||||
: TraitExpression<Superclass, OwnTraits, AllTraits>
|
|
||||||
)
|
)
|
||||||
|
|
||||||
|
|
||||||
class TraitExpressionBuilder<
|
class TraitExpressionBuilder<
|
||||||
Superclass extends AbstractClass<{}>,
|
Superclass extends AbstractClass<object>,
|
||||||
const OwnTraits extends Trait<any, any, any>[],
|
const Traits extends Trait<any, any, any, any>[],
|
||||||
const AllTraits extends Trait<any, any, any>[],
|
|
||||||
> {
|
> {
|
||||||
constructor(private expression: TraitExpression<Superclass, OwnTraits, AllTraits>) {}
|
constructor(
|
||||||
|
private readonly expressionSuperclass: Superclass,
|
||||||
|
private readonly expressionTraits: Traits,
|
||||||
|
) {}
|
||||||
|
|
||||||
extends<
|
extends<
|
||||||
Super extends AbstractClass<any>
|
Super extends AbstractClass<object>
|
||||||
>(
|
>(
|
||||||
superclass: Super
|
superclass: Super
|
||||||
) {
|
) {
|
||||||
return new TraitExpressionBuilder(
|
return new TraitExpressionBuilder(
|
||||||
new TraitExpression(
|
|
||||||
superclass,
|
superclass,
|
||||||
this.expression.ownTraits,
|
this.expressionTraits,
|
||||||
this.expression.allTraits,
|
|
||||||
)
|
|
||||||
)
|
)
|
||||||
}
|
}
|
||||||
|
|
||||||
expresses<
|
expresses<
|
||||||
const Traits extends Trait<any, any, any>[]
|
const T extends Trait<
|
||||||
>(
|
TraitExpression<
|
||||||
...traits: Traits
|
typeof TraitExpression.NullSuperclass,
|
||||||
): TraitExpressionBuilder<
|
Trait<any, any, any, any>[]
|
||||||
Superclass,
|
>,
|
||||||
[...OwnTraits, ...Traits],
|
any,
|
||||||
[...AllTraits, ...SpreadSupertraits<Traits>]
|
|
||||||
> {
|
|
||||||
return new TraitExpressionBuilder(
|
|
||||||
new TraitExpression(
|
|
||||||
this.expression.superclass,
|
|
||||||
[...this.expression.ownTraits, ...traits],
|
|
||||||
[...this.expression.allTraits, ...this.spreadSupertraits(traits)],
|
|
||||||
)
|
|
||||||
)
|
|
||||||
}
|
|
||||||
|
|
||||||
private spreadSupertraits<
|
|
||||||
const Traits extends Trait<
|
|
||||||
TraitExpression<any, any, Trait<any, any, any>[]>,
|
|
||||||
any,
|
any,
|
||||||
any
|
any
|
||||||
>[]
|
>[]
|
||||||
>(
|
>(
|
||||||
traits: Traits
|
...traits: T
|
||||||
) {
|
) {
|
||||||
return traits.flatMap(trait => [
|
return new TraitExpressionBuilder(
|
||||||
...trait.supertraits.allTraits,
|
this.expressionSuperclass,
|
||||||
|
|
||||||
|
uniq([
|
||||||
|
...this.expressionTraits,
|
||||||
|
...traits.flatMap(trait => [
|
||||||
|
...trait.superExpression.traits,
|
||||||
trait,
|
trait,
|
||||||
]) as SpreadSupertraits<Traits>
|
]),
|
||||||
|
]) as [...Traits, ...SpreadSupertraits<T>],
|
||||||
|
)
|
||||||
}
|
}
|
||||||
|
|
||||||
build() {
|
build() {
|
||||||
return this.expression as BuildTraitExpression<Superclass, OwnTraits, AllTraits>
|
return new TraitExpression(
|
||||||
|
this.expressionSuperclass,
|
||||||
|
this.expressionTraits,
|
||||||
|
) as BuildTraitExpression<Superclass, Traits>
|
||||||
}
|
}
|
||||||
|
|
||||||
then<V>(fn: (expression: ReturnType<typeof this.build>) => V): V {
|
then<V>(fn: (expression: ReturnType<typeof this.build>) => V): V {
|
||||||
return fn(this.build())
|
return fn(this.build())
|
||||||
}
|
}
|
||||||
|
|
||||||
|
buildAnyway() {
|
||||||
|
return new TraitExpression(
|
||||||
|
this.expressionSuperclass,
|
||||||
|
this.expressionTraits,
|
||||||
|
)
|
||||||
}
|
}
|
||||||
|
|
||||||
export const expression = new TraitExpressionBuilder(emptyTraitExpression)
|
thenAnyway<V>(fn: (expression: ReturnType<typeof this.buildAnyway>) => V): V {
|
||||||
|
return fn(this.buildAnyway())
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
export const expression = new TraitExpressionBuilder(TraitExpression.NullSuperclass, [])
|
||||||
|
|||||||
@@ -1,10 +0,0 @@
|
|||||||
import { Opaque } from "type-fest"
|
|
||||||
|
|
||||||
|
|
||||||
export type AbstractTag = "@thilawyn/traitify-ts/Abstract"
|
|
||||||
|
|
||||||
export function abstract<
|
|
||||||
Abstract extends {} = {}
|
|
||||||
>() {
|
|
||||||
return {} as Opaque<Abstract, AbstractTag>
|
|
||||||
}
|
|
||||||
@@ -1,4 +1,4 @@
|
|||||||
export { trait, type Trait } from "./Trait"
|
export { Trait, TraitClass, TraitConcreteClass, TraitInstance, TraitStaticMembers, TraitTuple } from "./Trait"
|
||||||
export { Implements, type TraitExpression } from "./TraitExpression"
|
export { TraitBuilder, trait } from "./TraitBuilder"
|
||||||
|
export { Implements, ImplementsStatic, TraitExpression, TraitExpressionClass, TraitExpressionConcreteClass, TraitExpressionInstance, TraitExpressionStaticMembers } from "./TraitExpression"
|
||||||
export { expression } from "./TraitExpressionBuilder"
|
export { expression } from "./TraitExpressionBuilder"
|
||||||
export { abstract } from "./abstract"
|
|
||||||
|
|||||||
77
src/tests.ts
77
src/tests.ts
@@ -1,67 +1,61 @@
|
|||||||
import { Trait, trait } from "./Trait"
|
import { TraitClass } from "./Trait"
|
||||||
import { Implements } from "./TraitExpression"
|
import { trait } from "./TraitBuilder"
|
||||||
|
import { Implements, ImplementsStatic, TraitExpressionClass } from "./TraitExpression"
|
||||||
import { expression } from "./TraitExpressionBuilder"
|
import { expression } from "./TraitExpressionBuilder"
|
||||||
import { abstract } from "./abstract"
|
|
||||||
|
|
||||||
|
|
||||||
const PrintsHelloOnNew = trait(
|
const PrintsHelloOnNew = trait
|
||||||
abstract(),
|
.implement(Super => class PrintsHelloOnNew extends Super {
|
||||||
Super => class PrintsHelloOnNew extends Super {
|
|
||||||
static readonly isPrintsHelloOnNew = true
|
static readonly isPrintsHelloOnNew = true
|
||||||
|
|
||||||
constructor(...args: any[]) {
|
constructor(...args: any[]) {
|
||||||
super(...args)
|
super(...args)
|
||||||
console.log("Hello!")
|
console.log("Hello!")
|
||||||
}
|
}
|
||||||
},
|
})
|
||||||
)
|
.build()
|
||||||
type PrintsHelloOnNewClass = Trait.Class<typeof PrintsHelloOnNew>
|
|
||||||
|
|
||||||
const Identifiable = <ID>() => trait(
|
type PrintsHelloOnNewClass = TraitClass<typeof PrintsHelloOnNew>
|
||||||
abstract<{ readonly id: ID }>(),
|
|
||||||
Super => class Identifiable extends Super {
|
const Identifiable = <ID>() => trait
|
||||||
|
.abstract(Super => class extends Super {
|
||||||
|
declare readonly id: ID
|
||||||
|
})
|
||||||
|
.implement(Super => class Identifiable extends Super {
|
||||||
equals(el: Identifiable) {
|
equals(el: Identifiable) {
|
||||||
return this.id === el.id
|
return this.id === el.id
|
||||||
}
|
}
|
||||||
},
|
})
|
||||||
)
|
.build()
|
||||||
|
|
||||||
const StatefulSubscription = trait(
|
const StatefulSubscription = trait
|
||||||
abstract<{
|
.abstract(Super => class extends Super {
|
||||||
readonly isStatefulSubscription: true
|
declare readonly isStatefulSubscription: true
|
||||||
readonly status: (
|
declare readonly status: (
|
||||||
{ _tag: "awaitingPayment" } |
|
{ _tag: "awaitingPayment" } |
|
||||||
{ _tag: "active", activeSince: Date, expiresAt?: Date } |
|
{ _tag: "active", activeSince: Date, expiresAt?: Date } |
|
||||||
{ _tag: "expired", expiredSince: Date }
|
{ _tag: "expired", expiredSince: Date }
|
||||||
)
|
)
|
||||||
}>(),
|
})
|
||||||
|
.build()
|
||||||
|
|
||||||
Super => class StatefulSubscription extends Super {},
|
type StatefulSubscriptionClass = TraitClass<typeof StatefulSubscription>
|
||||||
)
|
|
||||||
type StatefulSubscriptionClass = Trait.Class<typeof StatefulSubscription>
|
|
||||||
|
|
||||||
const ActiveStatefulSubscription = expression
|
const ActiveStatefulSubscription = expression
|
||||||
.expresses(StatefulSubscription)
|
.expresses(StatefulSubscription)
|
||||||
.build()
|
.build()
|
||||||
.subtrait(
|
.subtrait()
|
||||||
exp => {
|
.abstract(Super => class extends Super {
|
||||||
interface IActiveStatefulSubscription extends Implements<typeof exp> {
|
declare readonly isActiveStatefulSubscription: true
|
||||||
readonly isActiveStatefulSubscription: true
|
declare readonly status: { _tag: "active", activeSince: Date, expiresAt?: Date }
|
||||||
readonly status: { _tag: "active", activeSince: Date, expiresAt?: Date }
|
})
|
||||||
}
|
.build()
|
||||||
|
|
||||||
return abstract<IActiveStatefulSubscription>()
|
|
||||||
},
|
|
||||||
|
|
||||||
Super => class ActiveStatefulSubscription extends Super {},
|
|
||||||
)
|
|
||||||
|
|
||||||
type ActiveStatefulSubscriptionClass = Trait.Class<typeof ActiveStatefulSubscription>
|
|
||||||
|
|
||||||
|
type ActiveStatefulSubscriptionClass = TraitClass<typeof ActiveStatefulSubscription>
|
||||||
|
|
||||||
class TestSuperclass {
|
class TestSuperclass {
|
||||||
// id: number = 69
|
// id: number = 69
|
||||||
// static test = 69
|
static test = 69
|
||||||
}
|
}
|
||||||
|
|
||||||
const exp = expression
|
const exp = expression
|
||||||
@@ -75,7 +69,10 @@ const exp = expression
|
|||||||
.build()
|
.build()
|
||||||
|
|
||||||
type Abs = Implements<typeof exp>
|
type Abs = Implements<typeof exp>
|
||||||
|
type AbsStatic = ImplementsStatic<typeof exp>
|
||||||
|
type ExpClass = TraitExpressionClass<typeof exp>
|
||||||
|
|
||||||
|
@exp.implementsStatic
|
||||||
class User extends exp.extends implements Implements<typeof exp> {
|
class User extends exp.extends implements Implements<typeof exp> {
|
||||||
readonly isStatefulSubscription: true = true
|
readonly isStatefulSubscription: true = true
|
||||||
readonly isActiveStatefulSubscription: true = true
|
readonly isActiveStatefulSubscription: true = true
|
||||||
@@ -84,3 +81,9 @@ class User extends exp.extends implements Implements<typeof exp> {
|
|||||||
}
|
}
|
||||||
|
|
||||||
console.log(new User())
|
console.log(new User())
|
||||||
|
|
||||||
|
|
||||||
|
// type T = NonExtendableKeys<[
|
||||||
|
// { prout: "gneugneu" },
|
||||||
|
// { prout: string },
|
||||||
|
// ]>
|
||||||
|
|||||||
@@ -1,25 +1,71 @@
|
|||||||
import { Call, ComposeLeft, Fn, Match, Tuples } from "hotscript"
|
|
||||||
import { CommonKeys } from "."
|
import { CommonKeys } from "."
|
||||||
|
|
||||||
|
|
||||||
type ExtendReducer<Super, Self> = (
|
// type ExtendReducer<Super, Self> = (
|
||||||
Pick<Self, CommonKeys<Self, Super>> extends Pick<Super, CommonKeys<Self, Super>>
|
// Pick<Self, CommonKeys<Self, Super>> extends Pick<Super, CommonKeys<Self, Super>>
|
||||||
? Omit<Super, CommonKeys<Self, Super>> & Self
|
// ? Omit<Super, CommonKeys<Self, Super>> & Self
|
||||||
|
// : never
|
||||||
|
// )
|
||||||
|
// interface ExtendReducerFn extends Fn {
|
||||||
|
// return: ExtendReducer<this["arg0"], this["arg1"]>
|
||||||
|
// }
|
||||||
|
// export type ExtendFn = Tuples.Reduce<ExtendReducerFn, {}>
|
||||||
|
|
||||||
|
// export type ExtendableFn = ComposeLeft<[
|
||||||
|
// ExtendFn,
|
||||||
|
// Match<[
|
||||||
|
// Match.With<never, false>,
|
||||||
|
// Match.With<any, true>,
|
||||||
|
// ]>
|
||||||
|
// ]>
|
||||||
|
|
||||||
|
|
||||||
|
// TODO: use OverrideProperties from type-fest?
|
||||||
|
export type Extend<T extends readonly object[]> = (
|
||||||
|
T extends [
|
||||||
|
infer Super,
|
||||||
|
infer Self,
|
||||||
|
...infer Rest extends object[],
|
||||||
|
]
|
||||||
|
? Pick<Self, CommonKeys<Self, Super>> extends Pick<Super, CommonKeys<Self, Super>>
|
||||||
|
? Extend<[
|
||||||
|
Omit<Super, CommonKeys<Self, Super>> & Self,
|
||||||
|
...Rest,
|
||||||
|
]>
|
||||||
: never
|
: never
|
||||||
|
: T extends [infer Self]
|
||||||
|
? Self
|
||||||
|
: {}
|
||||||
)
|
)
|
||||||
interface ExtendReducerFn extends Fn {
|
|
||||||
return: ExtendReducer<this["arg0"], this["arg1"]>
|
|
||||||
}
|
|
||||||
|
|
||||||
export type ExtendFn = Tuples.Reduce<ExtendReducerFn, {}>
|
export type Extendable<T extends readonly object[]> = (
|
||||||
export type Extend<T extends {}[]> = Call<ExtendFn, T>
|
T extends [
|
||||||
|
infer Super,
|
||||||
|
infer Self,
|
||||||
export type ExtendableFn = ComposeLeft<[
|
...infer Rest extends object[],
|
||||||
ExtendFn,
|
]
|
||||||
Match<[
|
? Pick<Self, CommonKeys<Self, Super>> extends Pick<Super, CommonKeys<Self, Super>>
|
||||||
Match.With<never, false>,
|
? Extendable<[
|
||||||
Match.With<any, true>,
|
Omit<Super, CommonKeys<Self, Super>> & Self,
|
||||||
|
...Rest,
|
||||||
]>
|
]>
|
||||||
|
: false
|
||||||
|
: true
|
||||||
|
)
|
||||||
|
|
||||||
|
export type NonExtendableKeys<T extends readonly object[]> = (
|
||||||
|
T extends [
|
||||||
|
infer Super extends object,
|
||||||
|
infer Self extends object,
|
||||||
|
...infer Rest extends object[],
|
||||||
|
]
|
||||||
|
? {[K in keyof Super & keyof Self]: Self[K] extends Super[K]
|
||||||
|
? never
|
||||||
|
: K
|
||||||
|
}[keyof Super & keyof Self]
|
||||||
|
| NonExtendableKeys<[
|
||||||
|
Super & Self,
|
||||||
|
...Rest,
|
||||||
]>
|
]>
|
||||||
export type Extendable<T extends {}[]> = Call<ExtendableFn, T>
|
: void
|
||||||
|
)
|
||||||
|
|||||||
@@ -1,5 +1,4 @@
|
|||||||
import { Fn } from "hotscript"
|
import { AbstractClass } from "type-fest"
|
||||||
import { AbstractClass, Simplify } from "type-fest"
|
|
||||||
|
|
||||||
|
|
||||||
/**
|
/**
|
||||||
@@ -9,10 +8,6 @@ import { AbstractClass, Simplify } from "type-fest"
|
|||||||
*/
|
*/
|
||||||
export type CommonKeys<A, B> = Extract<keyof A, keyof B>
|
export type CommonKeys<A, B> = Extract<keyof A, keyof B>
|
||||||
|
|
||||||
export interface SimplifyFn extends Fn {
|
|
||||||
return: Simplify<this["arg0"]>
|
|
||||||
}
|
|
||||||
|
|
||||||
/**
|
/**
|
||||||
* Represents the static members of a class.
|
* Represents the static members of a class.
|
||||||
* @template Class - A class extending AbstractClass.
|
* @template Class - A class extending AbstractClass.
|
||||||
@@ -20,6 +15,3 @@ export interface SimplifyFn extends Fn {
|
|||||||
export type StaticMembers<Class extends AbstractClass<any>> = (
|
export type StaticMembers<Class extends AbstractClass<any>> = (
|
||||||
Omit<Class, "prototype">
|
Omit<Class, "prototype">
|
||||||
)
|
)
|
||||||
export interface StaticMembersFn extends Fn {
|
|
||||||
return: StaticMembers<this["arg0"]>
|
|
||||||
}
|
|
||||||
|
|||||||
Reference in New Issue
Block a user