Trait applier super tag as symbol
All checks were successful
continuous-integration/drone/push Build is passing

This commit is contained in:
Julien Valverdé
2024-02-06 22:02:26 +01:00
parent da17765276
commit 2bfb770e05
4 changed files with 12 additions and 9 deletions

View File

@@ -1,10 +1,12 @@
import { Fn, Pipe, Tuples } from "hotscript"
import { AbstractClass, Class, Opaque } from "type-fest"
import { tag } from "type-fest/source/opaque"
import { AbstractTag, TraitExpression, emptyTraitExpression } from "."
import { ExtendFn, SimplifyFn, StaticMembers, StaticMembersFn } from "./util"
export const isTraitApplierSuper = Symbol("isTraitApplierSuper")
export type AddAbstractToImplClass<
ImplClass extends Class<{}, []>,
Abstract extends {},
@@ -17,14 +19,14 @@ export type AddAbstractToImplClass<
)
export type RemoveAbstractFromImplClass<
ImplClassWithAbstract extends Opaque<Class<Abstract, []>, TraitApplierSuperTag>,
ImplClassWithAbstract extends Class<Abstract, []> & { [isTraitApplierSuper]: true },
Abstract extends {},
> = (
Class<
Omit<InstanceType<ImplClassWithAbstract>, keyof Abstract>,
ConstructorParameters<ImplClassWithAbstract>
> &
Omit<StaticMembers<ImplClassWithAbstract>, typeof tag>
Omit<StaticMembers<ImplClassWithAbstract>, typeof isTraitApplierSuper>
)
@@ -139,10 +141,10 @@ export type TraitApplierSuperTag = "@thilawyn/traitify-ts/TraitApplierSuper"
export function trait<
Abstract extends {},
ImplClassWithAbstract extends Opaque<Class<Abstract, []>, TraitApplierSuperTag>,
ImplClassWithAbstract extends Class<Abstract, []> & { [isTraitApplierSuper]: true },
>(
abstract: Opaque<Abstract, AbstractTag>,
apply: (Super: Opaque<AbstractClass<Abstract>, TraitApplierSuperTag>) => ImplClassWithAbstract,
apply: (Super: AbstractClass<Abstract> & { [isTraitApplierSuper]: true }) => ImplClassWithAbstract,
) {
return new Trait(
emptyTraitExpression,

View File

@@ -1,6 +1,6 @@
import { Call, Fn, Pipe, Tuples } from "hotscript"
import { AbstractClass, Class, Opaque } from "type-fest"
import { AbstractTag, RemoveAbstractFromImplClass, Trait, TraitApplierSuperTag } from "."
import { AbstractTag, RemoveAbstractFromImplClass, Trait, TraitApplierSuperTag, isTraitApplierSuper } from "."
import { ExtendFn, SimplifyFn, StaticMembersFn } from "./util"
@@ -53,11 +53,11 @@ export class TraitExpression<
subtrait<
This extends TraitExpression<typeof TraitExpression.NullSuperclass, any, any>,
SubtraitAbstract extends Implements<This>,
SubtraitImplClassWithAbstract extends Opaque<Class<SubtraitAbstract, []>, TraitApplierSuperTag>,
SubtraitImplClassWithAbstract extends Class<SubtraitAbstract, []> & { [isTraitApplierSuper]: true },
>(
this: This,
abstract: (expression: This) => Opaque<SubtraitAbstract, AbstractTag>,
apply: (Super: Opaque<AbstractClass<SubtraitAbstract>, TraitApplierSuperTag>) => SubtraitImplClassWithAbstract,
apply: (Super: AbstractClass<SubtraitAbstract> & { [isTraitApplierSuper]: true }) => SubtraitImplClassWithAbstract,
) {
return new Trait(
this,

View File

@@ -5,4 +5,5 @@ export {
trait,
type Trait,
type TraitExpression,
isTraitApplierSuper,
} from "."