0.1.11 #11

Merged
Thilawyn merged 7 commits from next into master 2024-02-24 23:42:25 +01:00
Showing only changes of commit e2d5364487 - Show all commits

View File

@@ -6,34 +6,6 @@ import { Extend, StaticMembers } from "./util"
declare const implSuperSymbol: unique symbol 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< export class TraitBuilder<
SuperExpression extends TraitExpression< SuperExpression extends TraitExpression<
@@ -44,6 +16,8 @@ export class TraitBuilder<
StaticAbstract extends object, StaticAbstract extends object,
ImplClass extends AbstractClass<object, []>, ImplClass extends AbstractClass<object, []>,
> { > {
declare ["constructor"]: typeof TraitBuilder
constructor( constructor(
readonly traitSuperExpression: SuperExpression, readonly traitSuperExpression: SuperExpression,
readonly traitAbstract: Abstract, readonly traitAbstract: Abstract,
@@ -51,10 +25,11 @@ export class TraitBuilder<
readonly traitApply: (Super: AbstractClass<object>) => ImplClass, readonly traitApply: (Super: AbstractClass<object>) => ImplClass,
) {} ) {}
abstract<A extends Abstract>( abstract<A extends Abstract>(
_: (Super: AbstractClass<Abstract>) => AbstractClass<A, []> _: (Super: AbstractClass<Abstract>) => AbstractClass<A, []>
) { ) {
return new TraitBuilder( return new this.constructor(
this.traitSuperExpression, this.traitSuperExpression,
{} as Simplify<A>, {} as Simplify<A>,
this.traitStaticAbstract, this.traitStaticAbstract,
@@ -65,7 +40,7 @@ export class TraitBuilder<
staticAbstract<A extends StaticAbstract>( staticAbstract<A extends StaticAbstract>(
_: (Super: AbstractClass<StaticAbstract>) => AbstractClass<A, []> _: (Super: AbstractClass<StaticAbstract>) => AbstractClass<A, []>
) { ) {
return new TraitBuilder( return new this.constructor(
this.traitSuperExpression, this.traitSuperExpression,
this.traitAbstract, this.traitAbstract,
{} as Simplify<A>, {} as Simplify<A>,
@@ -74,11 +49,11 @@ export class TraitBuilder<
} }
implement< 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 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: ImplSuper<typeof this>) => ImplClassWithAbstract apply: (Super: TraitBuilder.ImplSuper<typeof this>) => ImplClassWithAbstract
) { ) {
return new TraitBuilder( return new this.constructor(
this.traitSuperExpression, this.traitSuperExpression,
this.traitAbstract, this.traitAbstract,
this.traitStaticAbstract, this.traitStaticAbstract,
@@ -103,6 +78,7 @@ export class TraitBuilder<
) )
} }
build() { build() {
return new Trait( return new Trait(
this.traitSuperExpression, this.traitSuperExpression,
@@ -113,6 +89,37 @@ export class TraitBuilder<
} }
} }
export namespace TraitBuilder {
export 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 const trait = new TraitBuilder( export const trait = new TraitBuilder(
new TraitExpression(TraitExpression.NullSuperclass, []), new TraitExpression(TraitExpression.NullSuperclass, []),
{} as object, {} as object,