Compare commits
50 Commits
master
...
8d2cb90f52
| Author | SHA1 | Date | |
|---|---|---|---|
|
|
8d2cb90f52 | ||
|
|
891db74032 | ||
|
|
1dd118d856 | ||
|
|
26daee364d | ||
|
|
74029493e8 | ||
|
|
bb5e85573c | ||
|
|
2d32e4807d | ||
|
|
f6d2d68eee | ||
|
|
781567a3bd | ||
|
|
ec914e0ffd | ||
|
|
1219cd7a89 | ||
|
|
0ff6b7896e | ||
|
|
8d29d9ba94 | ||
|
|
0d469fabeb | ||
|
|
8af604831b | ||
|
|
bdfc57481d | ||
|
|
956457ee18 | ||
|
|
be59a6e0ea | ||
|
|
c6b9bbe9cb | ||
|
|
c044051813 | ||
|
|
c1097ff2f2 | ||
|
|
3c40a0a96b | ||
|
|
a061bac1de | ||
|
|
acc26c7e0c | ||
|
|
58de52ffc0 | ||
|
|
7a449c73cc | ||
|
|
29148cd42b | ||
|
|
01d59f2cef | ||
|
|
939772c89c | ||
|
|
48f5f44d8f | ||
|
|
7eb2b950a8 | ||
|
|
2945cdb7ae | ||
|
|
288969a789 | ||
|
|
a82c978c9e | ||
|
|
a5b8a1c184 | ||
|
|
a0aa9446d6 | ||
|
|
11335f10cd | ||
|
|
ff3991ae45 | ||
|
|
e493331ff3 | ||
|
|
5f0d21d93b | ||
|
|
bb6172381f | ||
|
|
f59706d994 | ||
|
|
3aade48a7d | ||
|
|
754a5dc420 | ||
|
|
ecbf936f85 | ||
|
|
9834856e8b | ||
|
|
8b01a13d7c | ||
|
|
096d4a92e7 | ||
|
|
e46f78d93e | ||
|
|
f5ab00c34a |
@@ -16,8 +16,8 @@ local lint_step = {
|
|||||||
|
|
||||||
local build_step = {
|
local build_step = {
|
||||||
name: "build",
|
name: "build",
|
||||||
image: node_image,
|
image: bun_image,
|
||||||
commands: ["npm run build"],
|
commands: ["bun run build"],
|
||||||
};
|
};
|
||||||
|
|
||||||
local pack_step = {
|
local pack_step = {
|
||||||
|
|||||||
46
package.json
46
package.json
@@ -1,6 +1,6 @@
|
|||||||
{
|
{
|
||||||
"name": "@thilawyn/traitify-ts",
|
"name": "@thilawyn/traitify-ts",
|
||||||
"version": "0.1.24",
|
"version": "0.1.0",
|
||||||
"type": "module",
|
"type": "module",
|
||||||
"publishConfig": {
|
"publishConfig": {
|
||||||
"registry": "https://git.jvalver.de/api/packages/thilawyn/npm/"
|
"registry": "https://git.jvalver.de/api/packages/thilawyn/npm/"
|
||||||
@@ -8,58 +8,38 @@
|
|||||||
"files": [
|
"files": [
|
||||||
"./dist"
|
"./dist"
|
||||||
],
|
],
|
||||||
"types": "./dist/lib.d.ts",
|
|
||||||
"exports": {
|
"exports": {
|
||||||
".": {
|
".": {
|
||||||
"import": {
|
"import": {
|
||||||
"types": "./dist/lib.d.ts",
|
"types": "./dist/lib.d.mts",
|
||||||
"default": "./dist/lib.js"
|
"default": "./dist/lib.mjs"
|
||||||
},
|
},
|
||||||
"require": {
|
"require": {
|
||||||
"types": "./dist/lib.d.cts",
|
"types": "./dist/lib.d.cts",
|
||||||
"default": "./dist/lib.cjs"
|
"default": "./dist/lib.cjs"
|
||||||
}
|
}
|
||||||
},
|
|
||||||
"./effect": {
|
|
||||||
"import": {
|
|
||||||
"types": "./dist/effect/lib.d.ts",
|
|
||||||
"default": "./dist/effect/lib.js"
|
|
||||||
},
|
|
||||||
"require": {
|
|
||||||
"types": "./dist/effect/lib.d.cts",
|
|
||||||
"default": "./dist/effect/lib.cjs"
|
|
||||||
}
|
|
||||||
},
|
|
||||||
"./traitsUnique-DCJN7XEW": {
|
|
||||||
"import": {
|
|
||||||
"types": "./dist/traitsUnique-DCJN7XEW.d.ts"
|
|
||||||
},
|
|
||||||
"require": {
|
|
||||||
"types": "./dist/traitsUnique-DCJN7XEW.d.ts"
|
|
||||||
}
|
|
||||||
}
|
}
|
||||||
},
|
},
|
||||||
"scripts": {
|
"scripts": {
|
||||||
"build": "tsup",
|
"build": "rollup -c rollup.config.ts",
|
||||||
"lint:tsc": "tsc --noEmit",
|
"lint:tsc": "tsc --noEmit",
|
||||||
"clean:cache": "rm -f tsconfig.tsbuildinfo",
|
"clean:cache": "rm -f tsconfig.tsbuildinfo",
|
||||||
"clean:dist": "rm -rf dist",
|
"clean:dist": "rm -rf dist",
|
||||||
"clean:node": "rm -rf node_modules"
|
"clean:node": "rm -rf node_modules"
|
||||||
},
|
},
|
||||||
"dependencies": {
|
"dependencies": {
|
||||||
"remeda": "^1.61.0",
|
"hotscript": "^1.0.13",
|
||||||
"type-fest": "^4.18.2"
|
"type-fest": "^4.10.1"
|
||||||
},
|
},
|
||||||
"devDependencies": {
|
"devDependencies": {
|
||||||
|
"@rollup/plugin-node-resolve": "^15.2.3",
|
||||||
"bun-types": "latest",
|
"bun-types": "latest",
|
||||||
"npm-check-updates": "^16.14.20",
|
"npm-check-updates": "^16.14.14",
|
||||||
"npm-sort": "^0.0.4",
|
"npm-sort": "^0.0.4",
|
||||||
"tsup": "^8.0.2",
|
"rollup": "^4.9.6",
|
||||||
"tsx": "^4.10.1",
|
"rollup-plugin-cleanup": "^3.2.1",
|
||||||
"typescript": "^5.4.5"
|
"rollup-plugin-ts": "^3.4.5",
|
||||||
},
|
"tsx": "^4.7.0",
|
||||||
"optionalDependencies": {
|
"typescript": "^5.3.3"
|
||||||
"@effect/schema": "^0.67.0",
|
|
||||||
"effect": "^3.1.3"
|
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|||||||
34
rollup.config.ts
Normal file
34
rollup.config.ts
Normal file
@@ -0,0 +1,34 @@
|
|||||||
|
import { nodeResolve } from "@rollup/plugin-node-resolve"
|
||||||
|
import { defineConfig } from "rollup"
|
||||||
|
import cleanup from "rollup-plugin-cleanup"
|
||||||
|
import ts from "rollup-plugin-ts"
|
||||||
|
import pkg from "./package.json" assert { type: "json" }
|
||||||
|
|
||||||
|
|
||||||
|
export default defineConfig({
|
||||||
|
input: "src/index.ts",
|
||||||
|
|
||||||
|
output: [
|
||||||
|
{
|
||||||
|
file: pkg.exports["."].import.default,
|
||||||
|
format: "esm",
|
||||||
|
},
|
||||||
|
|
||||||
|
{
|
||||||
|
file: pkg.exports["."].require.default,
|
||||||
|
format: "cjs",
|
||||||
|
},
|
||||||
|
],
|
||||||
|
|
||||||
|
external: id => !/^[./]/.test(id),
|
||||||
|
|
||||||
|
plugins: [
|
||||||
|
nodeResolve(),
|
||||||
|
ts(),
|
||||||
|
|
||||||
|
cleanup({
|
||||||
|
comments: "jsdoc",
|
||||||
|
extensions: ["ts"],
|
||||||
|
}),
|
||||||
|
],
|
||||||
|
})
|
||||||
201
src/Trait.ts
201
src/Trait.ts
@@ -1,136 +1,117 @@
|
|||||||
import { AbstractConstructor, Constructor, Simplify } from "type-fest"
|
import { Fn } from "hotscript"
|
||||||
import { TraitExpression } from "./TraitExpression"
|
import { AbstractClass, Class, Opaque } from "type-fest"
|
||||||
import { Extend, StaticMembers } from "./util"
|
import { AbstractTag, TraitExpression, TraitExpressionAllTraits, TraitExpressionNullSuperclass, emptyTraitExpression } from "."
|
||||||
|
import { StaticMembers } from "./util"
|
||||||
|
|
||||||
|
|
||||||
|
type AddAbstractToImplClass<
|
||||||
|
ImplClass extends Class<{}, []>,
|
||||||
|
Abstract extends {},
|
||||||
|
> = (
|
||||||
|
Class<
|
||||||
|
InstanceType<ImplClass> & Abstract,
|
||||||
|
ConstructorParameters<ImplClass>
|
||||||
|
> &
|
||||||
|
StaticMembers<ImplClass>
|
||||||
|
)
|
||||||
|
|
||||||
|
type RemoveAbstractFromImplClass<
|
||||||
|
ImplClassWithAbstract extends Class<Abstract, []>,
|
||||||
|
Abstract extends {},
|
||||||
|
> = (
|
||||||
|
Class<
|
||||||
|
Omit<InstanceType<ImplClassWithAbstract>, keyof Abstract>,
|
||||||
|
ConstructorParameters<ImplClassWithAbstract>
|
||||||
|
> &
|
||||||
|
StaticMembers<ImplClassWithAbstract>
|
||||||
|
)
|
||||||
|
|
||||||
|
|
||||||
export class Trait<
|
export class Trait<
|
||||||
SuperExpression extends TraitExpression<
|
Super extends TraitExpression<typeof TraitExpressionNullSuperclass, Trait<any, any, any>[], Trait<any, any, any>[]>,
|
||||||
typeof TraitExpression.NullSuperclass,
|
Abstract extends {},
|
||||||
readonly Trait<any, any, any, any>[]
|
ImplClass extends Class<{}, []>,
|
||||||
>,
|
|
||||||
Abstract extends object,
|
|
||||||
StaticAbstract extends object,
|
|
||||||
ImplClass extends AbstractConstructor<object, []>,
|
|
||||||
> {
|
> {
|
||||||
constructor(
|
constructor(
|
||||||
readonly superExpression: SuperExpression,
|
readonly supertraits: Super,
|
||||||
readonly abstract: Abstract,
|
readonly abstract: Abstract,
|
||||||
readonly staticAbstract: StaticAbstract,
|
readonly apply: (Super: AbstractClass<{}>) => ImplClass,
|
||||||
readonly apply: (Super: AbstractConstructor<object>) => ImplClass,
|
|
||||||
) {}
|
) {}
|
||||||
}
|
}
|
||||||
|
|
||||||
export namespace Trait {
|
export namespace Trait {
|
||||||
export type SuperExpression<T> = (
|
export type OwnAbstract<T> = (
|
||||||
T extends Trait<infer SuperExpression, any, any, any>
|
T extends Trait<any, infer Abstract, any>
|
||||||
? SuperExpression
|
|
||||||
: never
|
|
||||||
)
|
|
||||||
|
|
||||||
export type Supertraits<T> = (
|
|
||||||
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 StaticAbstract<T> = (
|
export type OwnImplClass<T> = (
|
||||||
T extends Trait<any, any, infer StaticAbstract, any>
|
T extends Trait<any, any, infer ImplClass>
|
||||||
? 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 ImplInstance<T> = (
|
export type OwnImplInstance<T> = (
|
||||||
InstanceType<Trait.ImplClass<T>>
|
T extends Trait<any, any, infer ImplClass>
|
||||||
|
? InstanceType<ImplClass>
|
||||||
|
: never
|
||||||
)
|
)
|
||||||
|
export interface OwnImplInstanceFn extends Fn {
|
||||||
|
return: Trait.OwnImplInstance<this["arg0"]>
|
||||||
|
}
|
||||||
|
|
||||||
export type ImplStaticMembers<T> = (
|
export type OwnClass<T> = (
|
||||||
StaticMembers<Trait.ImplClass<T>>
|
T extends Trait<any, infer Abstract, infer ImplClass>
|
||||||
|
? AddAbstractToImplClass<ImplClass, Abstract>
|
||||||
|
: never
|
||||||
)
|
)
|
||||||
|
export interface OwnClassFn extends Fn {
|
||||||
|
return: Trait.OwnClass<this["arg0"]>
|
||||||
|
}
|
||||||
|
|
||||||
export type Instance<T> = (
|
export type OwnInstance<T> = (
|
||||||
Extend<[
|
T extends Trait<any, infer Abstract, infer ImplClass>
|
||||||
Trait.Abstract<T>,
|
? InstanceType<
|
||||||
Trait.ImplInstance<T>,
|
AddAbstractToImplClass<ImplClass, Abstract>
|
||||||
]>
|
>
|
||||||
|
: never
|
||||||
)
|
)
|
||||||
|
export interface OwnInstanceFn extends Fn {
|
||||||
|
return: Trait.OwnInstance<this["arg0"]>
|
||||||
|
}
|
||||||
|
|
||||||
export type Static<T> = (
|
export type Supertraits<T> = (
|
||||||
Extend<[
|
T extends Trait<infer Super, any, any>
|
||||||
Trait.StaticAbstract<T>,
|
? TraitExpressionAllTraits<Super>
|
||||||
Trait.ImplStaticMembers<T>,
|
: never
|
||||||
]>
|
)
|
||||||
|
export interface SupertraitsFn extends Fn {
|
||||||
|
return: Trait.Supertraits<this["arg0"]>
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
|
||||||
|
export type TraitApplierSuperTag = "@thilawyn/traitify-ts/TraitApplierSuper"
|
||||||
|
|
||||||
|
export function trait<
|
||||||
|
Abstract extends {},
|
||||||
|
ImplClassWithAbstract extends Class<Abstract, []>,
|
||||||
|
>(
|
||||||
|
abstract: Opaque<Abstract, AbstractTag>,
|
||||||
|
apply: (Super: Opaque<AbstractClass<Abstract>, TraitApplierSuperTag>) => (
|
||||||
|
Opaque<ImplClassWithAbstract, TraitApplierSuperTag>
|
||||||
|
),
|
||||||
|
) {
|
||||||
|
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 readonly []
|
|
||||||
? T[K]
|
|
||||||
: Trait.Abstract<T[K]>
|
|
||||||
}
|
|
||||||
|
|
||||||
export type MapStaticAbstract<T> = {
|
|
||||||
[K in keyof T]: K extends keyof readonly []
|
|
||||||
? T[K]
|
|
||||||
: Trait.StaticAbstract<T[K]>
|
|
||||||
}
|
|
||||||
|
|
||||||
export type MapImplClass<T> = {
|
|
||||||
[K in keyof T]: K extends keyof readonly []
|
|
||||||
? T[K]
|
|
||||||
: Trait.ImplClass<T[K]>
|
|
||||||
}
|
|
||||||
|
|
||||||
export type MapImplInstance<T> = {
|
|
||||||
[K in keyof T]: K extends keyof readonly []
|
|
||||||
? T[K]
|
|
||||||
: Trait.ImplInstance<T[K]>
|
|
||||||
}
|
|
||||||
|
|
||||||
export type MapImplStaticMembers<T> = {
|
|
||||||
[K in keyof T]: K extends keyof readonly []
|
|
||||||
? T[K]
|
|
||||||
: Trait.ImplStaticMembers<T[K]>
|
|
||||||
}
|
|
||||||
|
|
||||||
export type MapInstance<T> = {
|
|
||||||
[K in keyof T]: K extends keyof readonly []
|
|
||||||
? T[K]
|
|
||||||
: Trait.Instance<T[K]>
|
|
||||||
}
|
|
||||||
|
|
||||||
export type MapStaticMembers<T> = {
|
|
||||||
[K in keyof T]: K extends keyof readonly []
|
|
||||||
? T[K]
|
|
||||||
: Trait.Static<T[K]>
|
|
||||||
}
|
|
||||||
}
|
|
||||||
|
|
||||||
|
|
||||||
export type TraitClass<T extends Trait<any, any, any, any>> = (
|
|
||||||
AbstractConstructor<TraitInstance<T>, any[]> &
|
|
||||||
TraitStaticMembers<T>
|
|
||||||
)
|
|
||||||
|
|
||||||
export type TraitConcreteClass<T extends Trait<any, any, any, any>> = (
|
|
||||||
Constructor<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>>
|
|
||||||
)
|
|
||||||
|
|||||||
@@ -1,180 +0,0 @@
|
|||||||
import { AbstractConstructor, Constructor, Simplify } from "type-fest"
|
|
||||||
import { Trait } from "./Trait"
|
|
||||||
import { TraitExpression } from "./TraitExpression"
|
|
||||||
import { Extend, StaticMembers, type } from "./util"
|
|
||||||
|
|
||||||
|
|
||||||
declare const implSuperSymbol: unique symbol
|
|
||||||
|
|
||||||
|
|
||||||
export class TraitBuilder<
|
|
||||||
SuperExpression extends TraitExpression<
|
|
||||||
typeof TraitExpression.NullSuperclass,
|
|
||||||
readonly Trait<any, any, any, any>[]
|
|
||||||
>,
|
|
||||||
Abstract extends object,
|
|
||||||
StaticAbstract extends object,
|
|
||||||
ImplClass extends AbstractConstructor<object, []>,
|
|
||||||
> {
|
|
||||||
constructor(
|
|
||||||
readonly traitSuperExpression: SuperExpression,
|
|
||||||
readonly traitAbstract: Abstract,
|
|
||||||
readonly traitStaticAbstract: StaticAbstract,
|
|
||||||
readonly traitApply: (Super: AbstractConstructor<object>) => ImplClass,
|
|
||||||
) {}
|
|
||||||
|
|
||||||
|
|
||||||
abstract<A extends AbstractConstructor<Abstract> & StaticAbstract>(
|
|
||||||
_: (Super: AbstractConstructor<Abstract> & StaticAbstract) => A
|
|
||||||
) {
|
|
||||||
return new TraitBuilder(
|
|
||||||
this.traitSuperExpression,
|
|
||||||
{} as Simplify<InstanceType<A>>,
|
|
||||||
{} as Simplify<StaticMembers<A>>,
|
|
||||||
this.traitApply,
|
|
||||||
)
|
|
||||||
}
|
|
||||||
|
|
||||||
abstractType<A extends Abstract>(
|
|
||||||
_: (Super: Abstract) => typeof type<A>
|
|
||||||
) {
|
|
||||||
return new TraitBuilder(
|
|
||||||
this.traitSuperExpression,
|
|
||||||
{} as Simplify<A>,
|
|
||||||
this.traitStaticAbstract,
|
|
||||||
this.traitApply,
|
|
||||||
)
|
|
||||||
}
|
|
||||||
|
|
||||||
staticAbstractType<A extends StaticAbstract>(
|
|
||||||
_: (Super: StaticAbstract) => typeof type<A>
|
|
||||||
) {
|
|
||||||
return new TraitBuilder(
|
|
||||||
this.traitSuperExpression,
|
|
||||||
this.traitAbstract,
|
|
||||||
{} as Simplify<A>,
|
|
||||||
this.traitApply,
|
|
||||||
)
|
|
||||||
}
|
|
||||||
|
|
||||||
implement<
|
|
||||||
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: TraitBuilder.ImplSuper<typeof this>) => ImplClassWithAbstract
|
|
||||||
) {
|
|
||||||
return new TraitBuilder(
|
|
||||||
this.traitSuperExpression,
|
|
||||||
this.traitAbstract,
|
|
||||||
this.traitStaticAbstract,
|
|
||||||
|
|
||||||
apply as unknown as (Super: AbstractConstructor<object>) => (
|
|
||||||
AbstractConstructor<
|
|
||||||
Simplify<
|
|
||||||
Omit<
|
|
||||||
InstanceType<ImplClassWithAbstract>,
|
|
||||||
keyof Abstract | "constructor"
|
|
||||||
>
|
|
||||||
>
|
|
||||||
> &
|
|
||||||
|
|
||||||
Simplify<
|
|
||||||
Omit<
|
|
||||||
StaticMembers<ImplClassWithAbstract>,
|
|
||||||
keyof StaticAbstract | typeof implSuperSymbol
|
|
||||||
>
|
|
||||||
>
|
|
||||||
),
|
|
||||||
)
|
|
||||||
}
|
|
||||||
|
|
||||||
|
|
||||||
build() {
|
|
||||||
return new Trait(
|
|
||||||
this.traitSuperExpression,
|
|
||||||
this.traitAbstract,
|
|
||||||
this.traitStaticAbstract,
|
|
||||||
this.traitApply,
|
|
||||||
)
|
|
||||||
}
|
|
||||||
}
|
|
||||||
|
|
||||||
export namespace TraitBuilder {
|
|
||||||
export type ImplSuper<This> = (
|
|
||||||
This extends TraitBuilder<
|
|
||||||
any,
|
|
||||||
infer Abstract,
|
|
||||||
infer StaticAbstract,
|
|
||||||
infer ImplClass
|
|
||||||
>
|
|
||||||
? (
|
|
||||||
AbstractConstructor<
|
|
||||||
Simplify<
|
|
||||||
Extend<[
|
|
||||||
Abstract,
|
|
||||||
InstanceType<ImplClass>,
|
|
||||||
]>
|
|
||||||
>
|
|
||||||
> &
|
|
||||||
|
|
||||||
Simplify<
|
|
||||||
Extend<[
|
|
||||||
StaticAbstract,
|
|
||||||
StaticMembers<ImplClass>,
|
|
||||||
]>
|
|
||||||
> &
|
|
||||||
{ readonly [implSuperSymbol]: true }
|
|
||||||
)
|
|
||||||
: never
|
|
||||||
)
|
|
||||||
}
|
|
||||||
|
|
||||||
|
|
||||||
export type ImplStatic<
|
|
||||||
ImplClass extends AbstractConstructor<object> & { readonly [implSuperSymbol]: true }
|
|
||||||
> = (
|
|
||||||
Simplify<
|
|
||||||
Omit<StaticMembers<ImplClass>, typeof implSuperSymbol>
|
|
||||||
>
|
|
||||||
)
|
|
||||||
|
|
||||||
export function implStaticThis<
|
|
||||||
ImplClass extends AbstractConstructor<object> & { readonly [implSuperSymbol]: true },
|
|
||||||
This extends AbstractConstructor<object>,
|
|
||||||
>(
|
|
||||||
_Impl: ImplClass,
|
|
||||||
thisArg: This,
|
|
||||||
) {
|
|
||||||
return thisArg as unknown as (
|
|
||||||
AbstractConstructor<
|
|
||||||
InstanceType<ImplClass> & InstanceType<This>,
|
|
||||||
ConstructorParameters<This>
|
|
||||||
> &
|
|
||||||
ImplStatic<ImplClass> &
|
|
||||||
StaticMembers<This>
|
|
||||||
)
|
|
||||||
}
|
|
||||||
|
|
||||||
export function implStaticInstantiableThis<
|
|
||||||
ImplClass extends AbstractConstructor<object> & { readonly [implSuperSymbol]: true },
|
|
||||||
This extends Constructor<object>,
|
|
||||||
>(
|
|
||||||
_Impl: ImplClass,
|
|
||||||
thisArg: This,
|
|
||||||
) {
|
|
||||||
return thisArg as unknown as (
|
|
||||||
Constructor<
|
|
||||||
InstanceType<ImplClass> & InstanceType<This>,
|
|
||||||
ConstructorParameters<This>
|
|
||||||
> &
|
|
||||||
ImplStatic<ImplClass> &
|
|
||||||
StaticMembers<This>
|
|
||||||
)
|
|
||||||
}
|
|
||||||
|
|
||||||
|
|
||||||
export const trait = new TraitBuilder(
|
|
||||||
new TraitExpression(TraitExpression.NullSuperclass, []),
|
|
||||||
{} as {},
|
|
||||||
{} as {},
|
|
||||||
Super => class extends Super {} as AbstractConstructor<{}>,
|
|
||||||
)
|
|
||||||
@@ -1,176 +1,107 @@
|
|||||||
import { AbstractConstructor, Constructor, Simplify } from "type-fest"
|
import { Call, Fn, Pipe, Tuples } from "hotscript"
|
||||||
import { Trait, TraitTuple } from "./Trait"
|
import { AbstractClass, Class, Opaque } from "type-fest"
|
||||||
import { TraitBuilder } from "./TraitBuilder"
|
import { AbstractTag, Trait, TraitApplierSuperTag } from "."
|
||||||
import { Extend, StaticMembers } from "./util"
|
import { ExtendFn, SimplifyFn, StaticMembersFn } from "./util"
|
||||||
|
|
||||||
|
|
||||||
export type TraitExpressionLike<
|
export class TraitExpressionNullSuperclass {}
|
||||||
Superclass extends AbstractConstructor<object>,
|
|
||||||
Traits extends readonly Trait<any, any, any, any>[],
|
|
||||||
> = {
|
|
||||||
readonly superclass: Superclass,
|
|
||||||
readonly traits: Traits,
|
|
||||||
}
|
|
||||||
|
|
||||||
|
|
||||||
export class TraitExpression<
|
export class TraitExpression<
|
||||||
Superclass extends AbstractConstructor<object>,
|
Super extends AbstractClass<{}>,
|
||||||
const Traits extends readonly Trait<any, any, any, any>[],
|
OwnTraits extends Trait<any, any, any>[],
|
||||||
>
|
AllTraits extends Trait<any, any, any>[],
|
||||||
implements TraitExpressionLike<Superclass, Traits> {
|
> {
|
||||||
constructor(
|
constructor(
|
||||||
readonly superclass: Superclass,
|
readonly superclass: Super,
|
||||||
readonly traits: Traits,
|
readonly ownTraits: OwnTraits,
|
||||||
|
readonly allTraits: AllTraits,
|
||||||
) {}
|
) {}
|
||||||
|
|
||||||
|
|
||||||
get extends(): (
|
get extends(): (
|
||||||
AbstractConstructor<
|
AbstractClass<
|
||||||
InstanceType<Superclass> & // Keep the instance of the superclass outside of any kind of type manipulation
|
Pipe<[
|
||||||
// as it can accidentely remove abstract properties
|
InstanceType<Super>,
|
||||||
Simplify<
|
...Call<Tuples.Map<Trait.OwnImplInstanceFn>, OwnTraits>,
|
||||||
Extend<TraitTuple.MapImplInstance<Traits>>
|
], [
|
||||||
>,
|
ExtendFn,
|
||||||
|
SimplifyFn,
|
||||||
|
]>,
|
||||||
|
|
||||||
ConstructorParameters<Superclass>
|
ConstructorParameters<Super>
|
||||||
> &
|
> &
|
||||||
|
|
||||||
Simplify<
|
Pipe<[
|
||||||
Extend<[
|
Super,
|
||||||
StaticMembers<Superclass>,
|
...Call<Tuples.Map<Trait.OwnImplClassFn>, OwnTraits>,
|
||||||
...TraitTuple.MapImplStaticMembers<Traits>,
|
], [
|
||||||
|
Tuples.Map<StaticMembersFn>,
|
||||||
|
ExtendFn,
|
||||||
|
SimplifyFn,
|
||||||
]>
|
]>
|
||||||
>
|
|
||||||
) {
|
) {
|
||||||
return this.traits.reduce(
|
return this.allTraits.reduce(
|
||||||
(previous, trait) => trait.apply(previous),
|
(previous, trait) => trait.apply(previous),
|
||||||
this.superclass,
|
this.superclass as Opaque<Super, TraitApplierSuperTag>,
|
||||||
) as any
|
) as any
|
||||||
}
|
}
|
||||||
|
|
||||||
|
|
||||||
staticImplements(_target: StaticImplements<typeof this>, _context: any) {}
|
|
||||||
|
|
||||||
get staticImplementsStage2() {
|
|
||||||
return (_target: StaticImplements<typeof this>) => {}
|
|
||||||
}
|
|
||||||
|
|
||||||
|
|
||||||
subtrait<
|
subtrait<
|
||||||
This extends TraitExpression<typeof TraitExpression.NullSuperclass, any>
|
SubtraitAbstract extends {},
|
||||||
|
SubtraitImplWithAbstract extends Class<{}>,
|
||||||
>(
|
>(
|
||||||
this: This
|
abstract: (
|
||||||
) {
|
abstract: Pipe<typeof this, [
|
||||||
return new TraitBuilder<This,
|
|
||||||
Simplify<
|
|
||||||
Extend<TraitTuple.MapAbstract<Traits>>
|
|
||||||
>,
|
|
||||||
Simplify<
|
|
||||||
Extend<TraitTuple.MapStaticAbstract<Traits>>
|
|
||||||
>,
|
|
||||||
|
|
||||||
AbstractConstructor<
|
|
||||||
Simplify<
|
|
||||||
Extend<TraitTuple.MapImplInstance<Traits>>
|
|
||||||
>
|
|
||||||
> &
|
|
||||||
Simplify<
|
|
||||||
Extend<TraitTuple.MapImplStaticMembers<Traits>>
|
|
||||||
>
|
|
||||||
>(
|
|
||||||
this,
|
|
||||||
{} as any,
|
|
||||||
{} as any,
|
|
||||||
Super => class extends Super {} as any,
|
|
||||||
)
|
|
||||||
}
|
|
||||||
}
|
|
||||||
|
|
||||||
export namespace TraitExpression {
|
|
||||||
const nullSuperclassSymbol = Symbol()
|
|
||||||
export class NullSuperclass {
|
|
||||||
static readonly [nullSuperclassSymbol]: true
|
|
||||||
constructor(..._args: any[]) {}
|
|
||||||
}
|
|
||||||
|
|
||||||
export type Superclass<T> = (
|
|
||||||
T extends TraitExpressionLike<infer Superclass, any>
|
|
||||||
? Superclass
|
|
||||||
: never
|
|
||||||
)
|
|
||||||
|
|
||||||
export type Traits<T> = (
|
|
||||||
T extends TraitExpressionLike<any, infer Traits>
|
|
||||||
? Traits
|
|
||||||
: never
|
|
||||||
)
|
|
||||||
}
|
|
||||||
|
|
||||||
|
|
||||||
export type Implements<
|
|
||||||
Exp extends TraitExpressionLike<any, readonly Trait<any, any, any, any>[]>
|
|
||||||
> = (
|
|
||||||
Simplify<
|
|
||||||
Extend<
|
|
||||||
TraitTuple.MapAbstract<
|
|
||||||
TraitExpression.Traits<Exp>
|
|
||||||
>
|
|
||||||
>
|
|
||||||
>
|
|
||||||
)
|
|
||||||
|
|
||||||
export type StaticImplements<
|
|
||||||
Exp extends TraitExpressionLike<any, readonly Trait<any, any, any, any>[]>
|
|
||||||
> = (
|
|
||||||
Simplify<
|
|
||||||
Extend<
|
|
||||||
TraitTuple.MapStaticAbstract<
|
|
||||||
TraitExpression.Traits<Exp>
|
|
||||||
>
|
|
||||||
>
|
|
||||||
>
|
|
||||||
)
|
|
||||||
|
|
||||||
|
|
||||||
export type TraitExpressionClass<
|
|
||||||
Exp extends TraitExpressionLike<any, readonly Trait<any, any, any, any>[]>
|
|
||||||
> = (
|
|
||||||
AbstractConstructor<
|
|
||||||
TraitExpressionInstance<Exp>,
|
|
||||||
ConstructorParameters<TraitExpression.Superclass<Exp>>
|
|
||||||
> &
|
|
||||||
TraitExpressionStaticMembers<Exp>
|
|
||||||
)
|
|
||||||
|
|
||||||
export type TraitExpressionInstantiableClass<
|
|
||||||
Exp extends TraitExpressionLike<any, readonly Trait<any, any, any, any>[]>
|
|
||||||
> = (
|
|
||||||
Constructor<
|
|
||||||
TraitExpressionInstance<Exp>,
|
|
||||||
ConstructorParameters<TraitExpression.Superclass<Exp>>
|
|
||||||
> &
|
|
||||||
TraitExpressionStaticMembers<Exp>
|
|
||||||
)
|
|
||||||
|
|
||||||
export type TraitExpressionInstance<
|
|
||||||
Exp extends TraitExpressionLike<any, readonly 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 TraitExpressionLike<any, readonly Trait<any, any, any, any>[]>
|
|
||||||
> = (
|
|
||||||
Simplify<
|
|
||||||
Extend<[
|
|
||||||
StaticMembers<TraitExpression.Superclass<Exp>>,
|
|
||||||
...TraitTuple.MapStaticMembers<TraitExpression.Traits<Exp>>,
|
|
||||||
]>
|
]>
|
||||||
>
|
) => Opaque<SubtraitAbstract, AbstractTag>,
|
||||||
|
|
||||||
|
// impl: ()
|
||||||
|
) {
|
||||||
|
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
export type TraitExpressionSuper<T> = (
|
||||||
|
T extends TraitExpression<infer Super, any, any>
|
||||||
|
? Super
|
||||||
|
: never
|
||||||
|
)
|
||||||
|
export interface TraitExpressionSuperFn extends Fn {
|
||||||
|
return: TraitExpressionSuper<this["arg0"]>
|
||||||
|
}
|
||||||
|
|
||||||
|
export type TraitExpressionOwnTraits<T> = (
|
||||||
|
T extends TraitExpression<any, infer OwnTraits, any>
|
||||||
|
? OwnTraits
|
||||||
|
: never
|
||||||
|
)
|
||||||
|
export interface TraitExpressionOwnTraitsFn extends Fn {
|
||||||
|
return: TraitExpressionOwnTraits<this["arg0"]>
|
||||||
|
}
|
||||||
|
|
||||||
|
export type TraitExpressionAllTraits<T> = (
|
||||||
|
T extends TraitExpression<any, any, infer AllTraits>
|
||||||
|
? AllTraits
|
||||||
|
: never
|
||||||
|
)
|
||||||
|
export interface TraitExpressionAllTraitsFn extends Fn {
|
||||||
|
return: TraitExpressionAllTraits<this["arg0"]>
|
||||||
|
}
|
||||||
|
|
||||||
|
|
||||||
|
export type Implements<Exp extends TraitExpression<any, any, any>> = (
|
||||||
|
Exp extends TraitExpression<any, infer AllTraits, any>
|
||||||
|
? Pipe<AllTraits, [
|
||||||
|
Tuples.Map<Trait.OwnAbstractFn>,
|
||||||
|
ExtendFn,
|
||||||
|
SimplifyFn,
|
||||||
|
]>
|
||||||
|
: never
|
||||||
|
)
|
||||||
|
|
||||||
|
|
||||||
|
export const emptyTraitExpression = new TraitExpression(
|
||||||
|
TraitExpressionNullSuperclass,
|
||||||
|
[] as const,
|
||||||
|
[] as const,
|
||||||
)
|
)
|
||||||
|
|||||||
@@ -1,150 +1,134 @@
|
|||||||
import { AbstractConstructor } from "type-fest"
|
import { Call, Fn, Pipe, Tuples } from "hotscript"
|
||||||
import { Trait, TraitTuple } from "./Trait"
|
import { AbstractClass } from "type-fest"
|
||||||
import { TraitExpression } from "./TraitExpression"
|
import { Trait, TraitExpression, emptyTraitExpression } from "."
|
||||||
import { SpreadSupertraits, spreadSupertraits } from "./spreadSupertraits"
|
import { ExtendableFn, StaticMembersFn } from "./util"
|
||||||
import { TraitsUnique, traitsUnique } from "./traitsUnique"
|
|
||||||
import { Extendable, StaticMembers } from "./util"
|
|
||||||
|
|
||||||
|
|
||||||
export class TraitExpressionBuilder<
|
class TraitExpressionBuilder<
|
||||||
Superclass extends AbstractConstructor<object>,
|
Super extends AbstractClass<{}>,
|
||||||
const Traits extends readonly Trait<any, any, any, any>[],
|
OwnTraits extends Trait<any, any, any>[],
|
||||||
|
AllTraits extends Trait<any, any, any>[],
|
||||||
> {
|
> {
|
||||||
constructor(
|
constructor(public expression: TraitExpression<Super, OwnTraits, AllTraits>) {}
|
||||||
readonly expressionSuperclass: Superclass,
|
|
||||||
readonly expressionTraits: Traits,
|
|
||||||
) {}
|
|
||||||
|
|
||||||
|
|
||||||
extends<
|
extends<
|
||||||
Super extends AbstractConstructor<object>
|
NewSuper extends AbstractClass<any>
|
||||||
>(
|
>(superclass: NewSuper) {
|
||||||
superclass: Super
|
|
||||||
) {
|
|
||||||
return new TraitExpressionBuilder(
|
return new TraitExpressionBuilder(
|
||||||
|
new TraitExpression(
|
||||||
superclass,
|
superclass,
|
||||||
this.expressionTraits,
|
this.expression.ownTraits,
|
||||||
|
this.expression.allTraits,
|
||||||
|
)
|
||||||
)
|
)
|
||||||
}
|
}
|
||||||
|
|
||||||
expresses<
|
expresses<
|
||||||
const T extends readonly Trait<
|
Traits extends Trait<any, any, any>[]
|
||||||
TraitExpression<
|
>(
|
||||||
typeof TraitExpression.NullSuperclass,
|
...traits: Traits
|
||||||
readonly Trait<any, any, any, any>[]
|
): TraitExpressionBuilder<
|
||||||
>,
|
Super,
|
||||||
any,
|
[...OwnTraits, ...Traits],
|
||||||
|
[...AllTraits, SpreadSupertraits<Traits>]
|
||||||
|
> {
|
||||||
|
return new TraitExpressionBuilder(
|
||||||
|
new TraitExpression(
|
||||||
|
this.expression.superclass,
|
||||||
|
[...this.expression.ownTraits, ...traits] as const,
|
||||||
|
[...this.expression.allTraits, ...this.spreadSupertraits(traits)] as const,
|
||||||
|
)
|
||||||
|
) as any
|
||||||
|
}
|
||||||
|
|
||||||
|
private spreadSupertraits<
|
||||||
|
Traits extends Trait<
|
||||||
|
TraitExpression<any, any, Trait<any, any, any>[]>,
|
||||||
any,
|
any,
|
||||||
any
|
any
|
||||||
>[]
|
>[]
|
||||||
>(
|
>(
|
||||||
...traits: T
|
traits: Traits
|
||||||
): TraitExpressionBuilder<
|
) {
|
||||||
Superclass,
|
return traits.flatMap(trait => [
|
||||||
TraitExpressionBuilder.ExpressesReturnTypeTraits<Traits, T>
|
...trait.supertraits.allTraits,
|
||||||
> {
|
trait,
|
||||||
return new TraitExpressionBuilder(
|
]) as SpreadSupertraits<Traits>
|
||||||
this.expressionSuperclass,
|
}
|
||||||
|
|
||||||
traitsUnique([
|
get() {
|
||||||
...this.expressionTraits,
|
return this.expression as GetTraitExpression<Super, OwnTraits, AllTraits>
|
||||||
...spreadSupertraits(traits),
|
}
|
||||||
]),
|
|
||||||
)
|
then<V>(fn: (expression: ReturnType<typeof this.get>) => V): V {
|
||||||
}
|
return fn(this.get())
|
||||||
|
}
|
||||||
expressesFirst<
|
}
|
||||||
const T extends readonly Trait<
|
|
||||||
TraitExpression<
|
|
||||||
typeof TraitExpression.NullSuperclass,
|
type SpreadSupertraits<Traits extends Trait<any, any, any>[]> = (
|
||||||
readonly Trait<any, any, any, any>[]
|
Call<
|
||||||
>,
|
Tuples.FlatMap<PrependTraitSupertraitsFn>,
|
||||||
any,
|
Traits
|
||||||
any,
|
>
|
||||||
any
|
|
||||||
>[]
|
|
||||||
>(
|
|
||||||
...traits: T
|
|
||||||
): TraitExpressionBuilder<
|
|
||||||
Superclass,
|
|
||||||
TraitExpressionBuilder.ExpressesFirstReturnTypeTraits<Traits, T>
|
|
||||||
> {
|
|
||||||
return new TraitExpressionBuilder(
|
|
||||||
this.expressionSuperclass,
|
|
||||||
|
|
||||||
traitsUnique([
|
|
||||||
...spreadSupertraits(traits),
|
|
||||||
...this.expressionTraits,
|
|
||||||
]),
|
|
||||||
)
|
)
|
||||||
|
interface PrependTraitSupertraitsFn extends Fn {
|
||||||
|
return: this["arg0"] extends Trait<infer Super, any, any>
|
||||||
|
? Super extends TraitExpression<any, any, infer AllTraits>
|
||||||
|
? [...AllTraits, this["arg0"]]
|
||||||
|
: never
|
||||||
|
: never
|
||||||
}
|
}
|
||||||
|
|
||||||
|
|
||||||
build() {
|
type AbstractMembersExtendable<
|
||||||
return new TraitExpression(
|
Super extends AbstractClass<{}>,
|
||||||
this.expressionSuperclass,
|
Traits extends Trait<any, any, any>[],
|
||||||
this.expressionTraits,
|
|
||||||
) as TraitExpressionBuilder.BuildTraitExpression<Superclass, Traits>
|
|
||||||
}
|
|
||||||
|
|
||||||
then<V>(fn: (expression: ReturnType<typeof this.build>) => V): V {
|
|
||||||
return fn(this.build())
|
|
||||||
}
|
|
||||||
|
|
||||||
buildAnyway() {
|
|
||||||
return new TraitExpression(
|
|
||||||
this.expressionSuperclass,
|
|
||||||
this.expressionTraits,
|
|
||||||
)
|
|
||||||
}
|
|
||||||
|
|
||||||
thenAnyway<V>(fn: (expression: ReturnType<typeof this.buildAnyway>) => V): V {
|
|
||||||
return fn(this.buildAnyway())
|
|
||||||
}
|
|
||||||
}
|
|
||||||
|
|
||||||
export namespace TraitExpressionBuilder {
|
|
||||||
export type ExpressesReturnTypeTraits<
|
|
||||||
Traits extends readonly Trait<any, any, any, any>[],
|
|
||||||
T extends readonly Trait<any, any, any, any>[],
|
|
||||||
> = (
|
> = (
|
||||||
TraitsUnique<readonly [
|
Call<ExtendableFn, [
|
||||||
...Traits,
|
InstanceType<Super>,
|
||||||
...SpreadSupertraits<T>,
|
...Call<Tuples.Map<Trait.OwnAbstractFn>, Traits>,
|
||||||
]>
|
]>
|
||||||
)
|
)
|
||||||
|
|
||||||
export type ExpressesFirstReturnTypeTraits<
|
type ImplInstanceExtendable<
|
||||||
Traits extends readonly Trait<any, any, any, any>[],
|
Super extends AbstractClass<{}>,
|
||||||
T extends readonly Trait<any, any, any, any>[],
|
Traits extends Trait<any, any, any>[],
|
||||||
> = (
|
> = (
|
||||||
TraitsUnique<readonly [
|
Call<ExtendableFn, [
|
||||||
...SpreadSupertraits<T>,
|
InstanceType<Super>,
|
||||||
...Traits,
|
...Call<Tuples.Map<Trait.OwnImplInstanceFn>, Traits>,
|
||||||
]>
|
]>
|
||||||
)
|
)
|
||||||
|
|
||||||
export type BuildTraitExpression<
|
type ImplStaticMembersExtendable<
|
||||||
Superclass extends AbstractConstructor<object>,
|
Super extends AbstractClass<{}>,
|
||||||
Traits extends readonly Trait<any, any, any, any>[],
|
Traits extends Trait<any, any, any>[],
|
||||||
> = (
|
> = (
|
||||||
Extendable<TraitTuple.MapAbstract<Traits>> extends false
|
Pipe<[
|
||||||
? "Type conflict between the traits abstract definitions."
|
Super,
|
||||||
: Extendable<TraitTuple.MapStaticAbstract<Traits>> extends false
|
...Call<Tuples.Map<Trait.OwnImplClassFn>, Traits>,
|
||||||
? "Type conflict between the traits static abstract definitions."
|
], [
|
||||||
: Extendable<[
|
Tuples.Map<StaticMembersFn>,
|
||||||
InstanceType<Superclass>,
|
ExtendableFn,
|
||||||
...TraitTuple.MapImplInstance<Traits>,
|
]>
|
||||||
]> extends false
|
)
|
||||||
? "Type conflict between the traits implementation instance and/or the superclass instance."
|
|
||||||
: Extendable<[
|
type GetTraitExpression<
|
||||||
StaticMembers<Superclass>,
|
Super extends AbstractClass<{}>,
|
||||||
...TraitTuple.MapImplStaticMembers<Traits>,
|
OwnTraits extends Trait<any, any, any>[],
|
||||||
]> extends false
|
AllTraits extends Trait<any, any, any>[],
|
||||||
|
> = (
|
||||||
|
Call<Tuples.IsEmpty, OwnTraits> extends true
|
||||||
|
? "Cannot express an empty list of traits."
|
||||||
|
: AbstractMembersExtendable<Super, OwnTraits> extends false
|
||||||
|
? "Type conflict between the traits abstract members and/or the superclass instance."
|
||||||
|
: ImplInstanceExtendable<Super, OwnTraits> extends false
|
||||||
|
? "Type conflict between the traits implementation instances and/or the superclass instance."
|
||||||
|
: ImplStaticMembersExtendable<Super, OwnTraits> extends false
|
||||||
? "Type conflict between the traits implementation static members and/or the superclass static members."
|
? "Type conflict between the traits implementation static members and/or the superclass static members."
|
||||||
: TraitExpression<Superclass, Traits>
|
: TraitExpression<Super, OwnTraits, AllTraits>
|
||||||
)
|
)
|
||||||
}
|
|
||||||
|
|
||||||
|
|
||||||
export const expression = new TraitExpressionBuilder(TraitExpression.NullSuperclass, [])
|
export const expression = new TraitExpressionBuilder(emptyTraitExpression)
|
||||||
|
|||||||
10
src/abstract.ts
Normal file
10
src/abstract.ts
Normal file
@@ -0,0 +1,10 @@
|
|||||||
|
import { Opaque } from "type-fest"
|
||||||
|
|
||||||
|
|
||||||
|
export type AbstractTag = "@thilawyn/traitify-ts/Abstract"
|
||||||
|
|
||||||
|
export function abstract<
|
||||||
|
Abstract extends {} = {}
|
||||||
|
>() {
|
||||||
|
return {} as Opaque<Abstract, AbstractTag>
|
||||||
|
}
|
||||||
@@ -1,83 +0,0 @@
|
|||||||
import { Schema as S } from "@effect/schema"
|
|
||||||
import { AbstractConstructor, Simplify } from "type-fest"
|
|
||||||
import { Trait, TraitTuple } from "../Trait"
|
|
||||||
import { StaticImplements, TraitExpressionLike } from "../TraitExpression"
|
|
||||||
import { Extend, StaticMembers } from "../util"
|
|
||||||
|
|
||||||
|
|
||||||
export class EffectSchemaTraitExpression<
|
|
||||||
Fields extends S.Struct.Fields,
|
|
||||||
I, R, C,
|
|
||||||
Inherited extends object,
|
|
||||||
Proto,
|
|
||||||
Static extends object,
|
|
||||||
|
|
||||||
const Traits extends readonly Trait<any, any, any, any>[],
|
|
||||||
|
|
||||||
const Mutability extends "Immutable" | "Mutable",
|
|
||||||
const EncodedMutability extends "Immutable" | "Mutable",
|
|
||||||
>
|
|
||||||
implements TraitExpressionLike<
|
|
||||||
S.Class<unknown, Fields, I, R, C, Inherited, Proto>,
|
|
||||||
Traits
|
|
||||||
> {
|
|
||||||
constructor(
|
|
||||||
readonly superclass: S.Class<unknown, Fields, I, R, C, Inherited, Proto>,
|
|
||||||
readonly superclassStatic: Static,
|
|
||||||
readonly traits: Traits,
|
|
||||||
readonly mutability: Mutability,
|
|
||||||
readonly encodedMutability: EncodedMutability,
|
|
||||||
) {}
|
|
||||||
|
|
||||||
|
|
||||||
extends<Self>(): (
|
|
||||||
AbstractConstructor<
|
|
||||||
Simplify<
|
|
||||||
ApplyMutability<S.Struct.Type<Fields>, Mutability> &
|
|
||||||
Extend<[
|
|
||||||
Omit<Inherited, keyof Fields>,
|
|
||||||
...TraitTuple.MapImplInstance<Traits>
|
|
||||||
]> &
|
|
||||||
Proto
|
|
||||||
>,
|
|
||||||
|
|
||||||
ConstructorParameters<S.Class<unknown, Fields, I, R, C, Inherited, Proto>>
|
|
||||||
> &
|
|
||||||
|
|
||||||
StaticMembers<
|
|
||||||
S.Class<
|
|
||||||
Self,
|
|
||||||
Fields,
|
|
||||||
ApplyMutability<I, EncodedMutability>,
|
|
||||||
R, C,
|
|
||||||
Inherited,
|
|
||||||
Proto
|
|
||||||
>
|
|
||||||
> &
|
|
||||||
Simplify<
|
|
||||||
Extend<[
|
|
||||||
Static,
|
|
||||||
...TraitTuple.MapImplStaticMembers<Traits>
|
|
||||||
]>
|
|
||||||
>
|
|
||||||
) {
|
|
||||||
return this.traits.reduce(
|
|
||||||
(previous, trait) => trait.apply(previous),
|
|
||||||
this.superclass,
|
|
||||||
) as any
|
|
||||||
}
|
|
||||||
|
|
||||||
|
|
||||||
staticImplements(_target: StaticImplements<typeof this>, _context: any) {}
|
|
||||||
|
|
||||||
get staticImplementsStage2() {
|
|
||||||
return (_target: StaticImplements<typeof this>) => {}
|
|
||||||
}
|
|
||||||
}
|
|
||||||
|
|
||||||
|
|
||||||
type ApplyMutability<T, Mutability extends "Immutable" | "Mutable"> = (
|
|
||||||
Mutability extends "Immutable"
|
|
||||||
? { +readonly [P in keyof T]: T[P] }
|
|
||||||
: { -readonly [P in keyof T]: T[P] }
|
|
||||||
)
|
|
||||||
@@ -1,206 +0,0 @@
|
|||||||
import { Schema as S } from "@effect/schema"
|
|
||||||
import { Simplify } from "type-fest"
|
|
||||||
import { Trait, TraitTuple } from "../Trait"
|
|
||||||
import { TraitExpression } from "../TraitExpression"
|
|
||||||
import { TraitExpressionBuilder } from "../TraitExpressionBuilder"
|
|
||||||
import { spreadSupertraits } from "../spreadSupertraits"
|
|
||||||
import { traitsUnique } from "../traitsUnique"
|
|
||||||
import { Extendable, StaticMembers } from "../util"
|
|
||||||
import { EffectSchemaTraitExpression } from "./EffectSchemaTraitExpression"
|
|
||||||
|
|
||||||
|
|
||||||
export class EffectSchemaInitialTraitExpressionBuilder {
|
|
||||||
class<
|
|
||||||
Fields extends S.Struct.Fields
|
|
||||||
>(
|
|
||||||
identifier: string,
|
|
||||||
fields: Fields,
|
|
||||||
annotations?: S.Annotations.Schema<unknown>,
|
|
||||||
) {
|
|
||||||
return new EffectSchemaTraitExpressionBuilder(
|
|
||||||
S.Class<unknown>(identifier)(fields, annotations),
|
|
||||||
{},
|
|
||||||
[],
|
|
||||||
"Immutable",
|
|
||||||
"Immutable",
|
|
||||||
)
|
|
||||||
}
|
|
||||||
|
|
||||||
taggedClass<
|
|
||||||
Tag extends string,
|
|
||||||
Fields extends S.Struct.Fields,
|
|
||||||
>(
|
|
||||||
tag: Tag,
|
|
||||||
fields: Fields,
|
|
||||||
annotations?: S.Annotations.Schema<unknown>,
|
|
||||||
) {
|
|
||||||
return new EffectSchemaTraitExpressionBuilder(
|
|
||||||
S.TaggedClass<unknown>()(tag, fields, annotations),
|
|
||||||
{},
|
|
||||||
[],
|
|
||||||
"Immutable",
|
|
||||||
"Immutable",
|
|
||||||
)
|
|
||||||
}
|
|
||||||
|
|
||||||
extends<
|
|
||||||
Super extends StaticMembers<S.Class<Self, Fields, I, R, C, Inherited, Proto>>,
|
|
||||||
Self extends object,
|
|
||||||
Fields extends S.Struct.Fields,
|
|
||||||
I, R, C,
|
|
||||||
Inherited extends object,
|
|
||||||
Proto,
|
|
||||||
|
|
||||||
NewFields extends S.Struct.Fields,
|
|
||||||
>(
|
|
||||||
superclass: Super | StaticMembers<S.Class<Self, Fields, I, R, C, Inherited, Proto>>,
|
|
||||||
identifier: string,
|
|
||||||
fields: NewFields,
|
|
||||||
annotations?: S.Annotations.Schema<unknown>,
|
|
||||||
) {
|
|
||||||
return new EffectSchemaTraitExpressionBuilder(
|
|
||||||
superclass.extend<unknown>(identifier)(fields, annotations),
|
|
||||||
|
|
||||||
{} as Simplify<
|
|
||||||
Omit<Super,
|
|
||||||
"prototype" | keyof S.Class<Self, Fields, I, R, C, Inherited, Proto>
|
|
||||||
>
|
|
||||||
>,
|
|
||||||
|
|
||||||
[],
|
|
||||||
"Immutable",
|
|
||||||
"Immutable",
|
|
||||||
)
|
|
||||||
}
|
|
||||||
}
|
|
||||||
|
|
||||||
|
|
||||||
export class EffectSchemaTraitExpressionBuilder<
|
|
||||||
Fields extends S.Struct.Fields,
|
|
||||||
I, R, C,
|
|
||||||
Inherited extends object,
|
|
||||||
Proto,
|
|
||||||
Static extends object,
|
|
||||||
|
|
||||||
const Traits extends readonly Trait<any, any, any, any>[],
|
|
||||||
|
|
||||||
const Mutability extends "Immutable" | "Mutable",
|
|
||||||
const EncodedMutability extends "Immutable" | "Mutable",
|
|
||||||
> {
|
|
||||||
constructor(
|
|
||||||
readonly expressionSuperclass: S.Class<unknown, Fields, I, R, C, Inherited, Proto>,
|
|
||||||
readonly expressionSuperclassStatic: Static,
|
|
||||||
readonly expressionTraits: Traits,
|
|
||||||
readonly expressionMutability: Mutability,
|
|
||||||
readonly expressionEncodedMutability: EncodedMutability,
|
|
||||||
) {}
|
|
||||||
|
|
||||||
|
|
||||||
mutable() {
|
|
||||||
return new EffectSchemaTraitExpressionBuilder(
|
|
||||||
this.expressionSuperclass,
|
|
||||||
this.expressionSuperclassStatic,
|
|
||||||
this.expressionTraits,
|
|
||||||
"Mutable",
|
|
||||||
this.expressionEncodedMutability,
|
|
||||||
)
|
|
||||||
}
|
|
||||||
|
|
||||||
immutable() {
|
|
||||||
return new EffectSchemaTraitExpressionBuilder(
|
|
||||||
this.expressionSuperclass,
|
|
||||||
this.expressionSuperclassStatic,
|
|
||||||
this.expressionTraits,
|
|
||||||
"Immutable",
|
|
||||||
this.expressionEncodedMutability,
|
|
||||||
)
|
|
||||||
}
|
|
||||||
|
|
||||||
mutableEncoded() {
|
|
||||||
return new EffectSchemaTraitExpressionBuilder(
|
|
||||||
this.expressionSuperclass,
|
|
||||||
this.expressionSuperclassStatic,
|
|
||||||
this.expressionTraits,
|
|
||||||
this.expressionMutability,
|
|
||||||
"Mutable",
|
|
||||||
)
|
|
||||||
}
|
|
||||||
|
|
||||||
immutableEncoded() {
|
|
||||||
return new EffectSchemaTraitExpressionBuilder(
|
|
||||||
this.expressionSuperclass,
|
|
||||||
this.expressionSuperclassStatic,
|
|
||||||
this.expressionTraits,
|
|
||||||
this.expressionMutability,
|
|
||||||
"Immutable",
|
|
||||||
)
|
|
||||||
}
|
|
||||||
|
|
||||||
|
|
||||||
expresses<
|
|
||||||
const T extends readonly Trait<
|
|
||||||
TraitExpression<
|
|
||||||
typeof TraitExpression.NullSuperclass,
|
|
||||||
readonly Trait<any, any, any, any>[]
|
|
||||||
>,
|
|
||||||
any,
|
|
||||||
any,
|
|
||||||
any
|
|
||||||
>[]
|
|
||||||
>(
|
|
||||||
...traits: T
|
|
||||||
) {
|
|
||||||
return new EffectSchemaTraitExpressionBuilder(
|
|
||||||
this.expressionSuperclass,
|
|
||||||
this.expressionSuperclassStatic,
|
|
||||||
|
|
||||||
traitsUnique([
|
|
||||||
...this.expressionTraits,
|
|
||||||
...spreadSupertraits(traits),
|
|
||||||
]) as TraitExpressionBuilder.ExpressesReturnTypeTraits<Traits, T>,
|
|
||||||
|
|
||||||
this.expressionMutability,
|
|
||||||
this.expressionEncodedMutability,
|
|
||||||
)
|
|
||||||
}
|
|
||||||
|
|
||||||
|
|
||||||
build(): (
|
|
||||||
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<typeof this.expressionSuperclass>,
|
|
||||||
...TraitTuple.MapImplInstance<Traits>,
|
|
||||||
]> extends false
|
|
||||||
? "Type conflict between the traits implementation instance and/or the superclass instance."
|
|
||||||
: Extendable<[
|
|
||||||
Static,
|
|
||||||
...TraitTuple.MapImplStaticMembers<Traits>,
|
|
||||||
]> extends false
|
|
||||||
? "Type conflict between the traits implementation static members and/or the superclass static members."
|
|
||||||
: EffectSchemaTraitExpression<
|
|
||||||
Fields,
|
|
||||||
I, R, C,
|
|
||||||
Inherited,
|
|
||||||
Proto,
|
|
||||||
Static,
|
|
||||||
|
|
||||||
Traits,
|
|
||||||
Mutability,
|
|
||||||
EncodedMutability
|
|
||||||
>
|
|
||||||
) {
|
|
||||||
return new EffectSchemaTraitExpression(
|
|
||||||
this.expressionSuperclass,
|
|
||||||
this.expressionSuperclassStatic,
|
|
||||||
this.expressionTraits,
|
|
||||||
this.expressionMutability,
|
|
||||||
this.expressionEncodedMutability,
|
|
||||||
) as any
|
|
||||||
}
|
|
||||||
}
|
|
||||||
|
|
||||||
|
|
||||||
export const effectSchemaExpression = new EffectSchemaInitialTraitExpressionBuilder()
|
|
||||||
@@ -1,2 +0,0 @@
|
|||||||
export { EffectSchemaTraitExpression } from "./EffectSchemaTraitExpression"
|
|
||||||
export { EffectSchemaInitialTraitExpressionBuilder, EffectSchemaTraitExpressionBuilder, effectSchemaExpression } from "./EffectSchemaTraitExpressionBuilder"
|
|
||||||
@@ -1,48 +0,0 @@
|
|||||||
import { Schema as S } from "@effect/schema"
|
|
||||||
import { Implements } from "../TraitExpression"
|
|
||||||
import { effectSchemaExpression } from "./EffectSchemaTraitExpressionBuilder"
|
|
||||||
|
|
||||||
|
|
||||||
type RequiredKeys<T> = {
|
|
||||||
[K in keyof T]-?: {} extends Pick<T, K> ? never : K
|
|
||||||
}[keyof T]
|
|
||||||
|
|
||||||
type InspectSchemaClass<T> = T extends S.Class<infer Self, infer Fields, infer I, infer R, infer C, infer Inherited, infer Proto>
|
|
||||||
? Inherited
|
|
||||||
: never
|
|
||||||
|
|
||||||
|
|
||||||
const userExp = effectSchemaExpression
|
|
||||||
.class("User", {
|
|
||||||
id: S.BigIntFromSelf,
|
|
||||||
role: S.Union(S.Literal("User"), S.Literal("Admin")),
|
|
||||||
})
|
|
||||||
.mutable()
|
|
||||||
.mutableEncoded()
|
|
||||||
.build()
|
|
||||||
|
|
||||||
@userExp.staticImplements
|
|
||||||
export class User extends userExp.extends<User>() implements Implements<typeof userExp> {
|
|
||||||
aMethodThatShouldBeInherited() {}
|
|
||||||
static aStaticMethodThatShouldBeInherited() {}
|
|
||||||
}
|
|
||||||
|
|
||||||
User.Encoded
|
|
||||||
|
|
||||||
const user = new User({ id: 0n, role: "User" })
|
|
||||||
user.id = 0n
|
|
||||||
|
|
||||||
|
|
||||||
const adminExp = effectSchemaExpression
|
|
||||||
.extends(User, "User", {
|
|
||||||
role: S.Literal("Admin")
|
|
||||||
})
|
|
||||||
// .immutable()
|
|
||||||
.build()
|
|
||||||
|
|
||||||
@adminExp.staticImplements
|
|
||||||
export class Admin extends adminExp.extends<Admin>() implements Implements<typeof adminExp> {
|
|
||||||
}
|
|
||||||
|
|
||||||
const admin = new Admin({ id: 1n, role: "Admin" })
|
|
||||||
// admin.role = "Admin"
|
|
||||||
4
src/index.ts
Normal file
4
src/index.ts
Normal file
@@ -0,0 +1,4 @@
|
|||||||
|
export * from "./Trait"
|
||||||
|
export * from "./TraitExpression"
|
||||||
|
export * from "./TraitExpressionBuilder"
|
||||||
|
export * from "./abstract"
|
||||||
@@ -1,5 +0,0 @@
|
|||||||
export { Trait, TraitClass, TraitConcreteClass, TraitInstance, TraitStaticMembers, TraitTuple } from "./Trait"
|
|
||||||
export { ImplStatic, TraitBuilder, implStaticInstantiableThis, implStaticThis, trait } from "./TraitBuilder"
|
|
||||||
export { Implements, StaticImplements, TraitExpression, TraitExpressionClass, TraitExpressionInstance, TraitExpressionInstantiableClass, TraitExpressionStaticMembers } from "./TraitExpression"
|
|
||||||
export { TraitExpressionBuilder, expression } from "./TraitExpressionBuilder"
|
|
||||||
export { type } from "./util"
|
|
||||||
@@ -1,35 +0,0 @@
|
|||||||
import { Trait } from "./Trait"
|
|
||||||
import { TraitExpression } from "./TraitExpression"
|
|
||||||
|
|
||||||
|
|
||||||
export function spreadSupertraits<
|
|
||||||
const T extends readonly Trait<
|
|
||||||
TraitExpression<
|
|
||||||
typeof TraitExpression.NullSuperclass,
|
|
||||||
readonly Trait<any, any, any, any>[]
|
|
||||||
>,
|
|
||||||
any,
|
|
||||||
any,
|
|
||||||
any
|
|
||||||
>[]
|
|
||||||
>(
|
|
||||||
traits: T
|
|
||||||
) {
|
|
||||||
return traits.flatMap(trait => [
|
|
||||||
...trait.superExpression.traits,
|
|
||||||
trait,
|
|
||||||
]) as readonly Trait<any, any, any, any>[] as SpreadSupertraits<T>
|
|
||||||
}
|
|
||||||
|
|
||||||
export type SpreadSupertraits<Traits> = (
|
|
||||||
Traits extends readonly [
|
|
||||||
infer El extends Trait<any, any, any, any>,
|
|
||||||
...infer Rest,
|
|
||||||
]
|
|
||||||
? readonly [
|
|
||||||
...Trait.Supertraits<El>,
|
|
||||||
El,
|
|
||||||
...SpreadSupertraits<Rest>,
|
|
||||||
]
|
|
||||||
: readonly []
|
|
||||||
)
|
|
||||||
101
src/tests.ts
101
src/tests.ts
@@ -1,96 +1,77 @@
|
|||||||
import { TraitClass } from "./Trait"
|
import { Implements, Trait, abstract, expression, trait } from "."
|
||||||
import { trait } from "./TraitBuilder"
|
|
||||||
import { Implements, StaticImplements, TraitExpressionClass } from "./TraitExpression"
|
|
||||||
import { expression } from "./TraitExpressionBuilder"
|
|
||||||
import { type } from "./util"
|
|
||||||
|
|
||||||
|
|
||||||
const PrintsHelloOnNew = trait
|
const PrintsHelloOnNew = trait(
|
||||||
.implement(Super => class PrintsHelloOnNew extends Super {
|
abstract(),
|
||||||
static readonly isPrintsHelloOnNew = true
|
Super => class PrintsHelloOnNew extends Super {
|
||||||
|
|
||||||
constructor(...args: any[]) {
|
constructor(...args: any[]) {
|
||||||
super(...args)
|
super(...args)
|
||||||
console.log("Hello!")
|
console.log("Hello!")
|
||||||
}
|
}
|
||||||
})
|
},
|
||||||
.build()
|
)
|
||||||
|
|
||||||
type PrintsHelloOnNewClass = TraitClass<typeof PrintsHelloOnNew>
|
const Identifiable = <ID>() => trait(
|
||||||
|
abstract<{ readonly id: ID }>(),
|
||||||
const Identifiable = <ID>() => trait
|
Super => class Identifiable extends Super {
|
||||||
// .abstract(Super => class extends Super {
|
|
||||||
// declare readonly id: ID
|
|
||||||
// })
|
|
||||||
.abstractType(Super => type<typeof Super & {
|
|
||||||
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 ImplementsIdentifiable = <ID>(defaultID: ID) => expression
|
const StatefulSubscription = trait(
|
||||||
.expresses(Identifiable<ID>())
|
abstract<{
|
||||||
.build()
|
readonly status: (
|
||||||
.subtrait()
|
|
||||||
.implement(Super => class ImplementsIdentifiable extends Super {
|
|
||||||
readonly id = defaultID
|
|
||||||
})
|
|
||||||
.build()
|
|
||||||
|
|
||||||
const StatefulSubscription = trait
|
|
||||||
.abstract(Super => class extends Super {
|
|
||||||
declare readonly isStatefulSubscription: true
|
|
||||||
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()
|
|
||||||
|
|
||||||
type StatefulSubscriptionClass = TraitClass<typeof StatefulSubscription>
|
Super => class StatefulSubscription extends Super {},
|
||||||
|
)
|
||||||
|
|
||||||
const ActiveStatefulSubscription = expression
|
// interface ActiveStatefulSubscriptionAbstractMembers extends TraitAbstractMembers<typeof StatefulSubscription> {
|
||||||
.expresses(StatefulSubscription)
|
// readonly status: { _tag: "active", activeSince: Date, expiresAt?: Date }
|
||||||
.build()
|
// }
|
||||||
.subtrait()
|
|
||||||
.abstract(Super => class extends Super {
|
// const ActiveStatefulSubscription = trait(
|
||||||
declare readonly isActiveStatefulSubscription: true
|
// abstract<ActiveStatefulSubscriptionAbstractMembers>(),
|
||||||
declare readonly status: { _tag: "active", activeSince: Date, expiresAt?: Date }
|
// Super => class ActiveStatefulSubscription extends Super {},
|
||||||
})
|
// )
|
||||||
.build()
|
|
||||||
|
|
||||||
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 builder = expression
|
||||||
.extends(TestSuperclass)
|
.extends(TestSuperclass)
|
||||||
.expresses(
|
.expresses(
|
||||||
PrintsHelloOnNew,
|
PrintsHelloOnNew,
|
||||||
Identifiable<bigint>(),
|
Identifiable<bigint>(),
|
||||||
// Identifiable<number>(),
|
// Identifiable<number>(),
|
||||||
// StatefulSubscription,
|
StatefulSubscription,
|
||||||
ActiveStatefulSubscription,
|
|
||||||
)
|
)
|
||||||
.build()
|
|
||||||
|
|
||||||
|
const exp = builder.get()
|
||||||
type Abs = Implements<typeof exp>
|
type Abs = Implements<typeof exp>
|
||||||
type AbsStatic = StaticImplements<typeof exp>
|
|
||||||
type ExpClass = TraitExpressionClass<typeof exp>
|
|
||||||
|
|
||||||
@exp.staticImplements
|
// exp.subtrait(
|
||||||
|
// s => {
|
||||||
|
// interface Subtrait extends (typeof s) {
|
||||||
|
|
||||||
|
// }
|
||||||
|
|
||||||
|
// return abstract<Subtrait>()
|
||||||
|
// },
|
||||||
|
// )
|
||||||
|
|
||||||
class User extends exp.extends implements Implements<typeof exp> {
|
class User extends exp.extends implements Implements<typeof exp> {
|
||||||
readonly isStatefulSubscription: true = true
|
declare status: { _tag: "awaitingPayment" } | { _tag: "active"; activeSince: Date; expiresAt?: Date | undefined } | { _tag: "expired"; expiredSince: Date }
|
||||||
readonly isActiveStatefulSubscription: true = true
|
|
||||||
declare status: { _tag: "active"; activeSince: Date; expiresAt?: Date | undefined }
|
|
||||||
id: bigint = -1n
|
id: bigint = -1n
|
||||||
}
|
}
|
||||||
|
|
||||||
|
|||||||
@@ -1,39 +0,0 @@
|
|||||||
import { unique } from "remeda"
|
|
||||||
import { IsEqual } from "type-fest"
|
|
||||||
import { Trait } from "./Trait"
|
|
||||||
import { TraitExpression } from "./TraitExpression"
|
|
||||||
|
|
||||||
|
|
||||||
export function traitsUnique<
|
|
||||||
const T extends readonly Trait<
|
|
||||||
TraitExpression<
|
|
||||||
typeof TraitExpression.NullSuperclass,
|
|
||||||
readonly Trait<any, any, any, any>[]
|
|
||||||
>,
|
|
||||||
any,
|
|
||||||
any,
|
|
||||||
any
|
|
||||||
>[]
|
|
||||||
>(
|
|
||||||
traits: T
|
|
||||||
) {
|
|
||||||
return unique(traits) as readonly Trait<any, any, any, any>[] as TraitsUnique<T>
|
|
||||||
}
|
|
||||||
|
|
||||||
export type TraitsUnique<Traits> = (
|
|
||||||
Traits extends readonly [
|
|
||||||
...infer Rest,
|
|
||||||
infer El extends Trait<any, any, any, any>,
|
|
||||||
]
|
|
||||||
? IsTraitInTupleFromRight<Rest, El> extends true
|
|
||||||
? TraitsUnique<Rest>
|
|
||||||
: readonly [...TraitsUnique<Rest>, El]
|
|
||||||
: readonly []
|
|
||||||
)
|
|
||||||
type IsTraitInTupleFromRight<Traits, T> = (
|
|
||||||
Traits extends readonly [...infer Rest, infer El]
|
|
||||||
? IsEqual<El, T> extends true
|
|
||||||
? true
|
|
||||||
: IsTraitInTupleFromRight<Rest, T>
|
|
||||||
: false
|
|
||||||
)
|
|
||||||
15
src/util/class.ts
Normal file
15
src/util/class.ts
Normal file
@@ -0,0 +1,15 @@
|
|||||||
|
import { Fn } from "hotscript"
|
||||||
|
import { AbstractClass } from "type-fest"
|
||||||
|
|
||||||
|
|
||||||
|
/**
|
||||||
|
* Represents the static members of a class.
|
||||||
|
* @template Class - A class extending AbstractClass.
|
||||||
|
*/
|
||||||
|
export type StaticMembers<Class extends AbstractClass<any>> = (
|
||||||
|
Omit<Class, "prototype">
|
||||||
|
)
|
||||||
|
|
||||||
|
export interface StaticMembersFn extends Fn {
|
||||||
|
return: StaticMembers<this["arg0"]>
|
||||||
|
}
|
||||||
@@ -1,71 +1,26 @@
|
|||||||
import { CommonKeys } from "."
|
import { Call, ComposeLeft, Fn, Match, Tuples } from "hotscript"
|
||||||
|
|
||||||
|
|
||||||
// type ExtendReducer<Super, Self> = (
|
type CommonKeys<A, B> = Extract<keyof A, keyof B>
|
||||||
// Pick<Self, CommonKeys<Self, Super>> extends Pick<Super, CommonKeys<Self, Super>>
|
|
||||||
// ? 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<[
|
type ExtendReducer<Super, Self> = (
|
||||||
// ExtendFn,
|
Pick<Self, CommonKeys<Self, Super>> extends Pick<Super, CommonKeys<Self, Super>>
|
||||||
// Match<[
|
? Omit<Super, CommonKeys<Self, Super>> & Self
|
||||||
// Match.With<never, false>,
|
|
||||||
// Match.With<any, true>,
|
|
||||||
// ]>
|
|
||||||
// ]>
|
|
||||||
|
|
||||||
|
|
||||||
// TODO: use OverrideProperties from type-fest?
|
|
||||||
export type Extend<T extends readonly object[]> = (
|
|
||||||
T extends readonly [
|
|
||||||
infer Super,
|
|
||||||
infer Self,
|
|
||||||
...infer Rest extends readonly object[],
|
|
||||||
]
|
|
||||||
? Pick<Self, CommonKeys<Self, Super>> extends Pick<Super, CommonKeys<Self, Super>>
|
|
||||||
? Extend<readonly [
|
|
||||||
Omit<Super, CommonKeys<Self, Super>> & Self,
|
|
||||||
...Rest,
|
|
||||||
]>
|
|
||||||
: never
|
: never
|
||||||
: T extends readonly [infer Self]
|
|
||||||
? Self
|
|
||||||
: {}
|
|
||||||
)
|
)
|
||||||
|
interface ExtendReducerFn extends Fn {
|
||||||
|
return: ExtendReducer<this["arg0"], this["arg1"]>
|
||||||
|
}
|
||||||
|
|
||||||
export type Extendable<T extends readonly object[]> = (
|
export type ExtendFn = Tuples.Reduce<ExtendReducerFn, {}>
|
||||||
T extends readonly [
|
export type Extend<T extends object[]> = Call<ExtendFn, T>
|
||||||
infer Super,
|
|
||||||
infer Self,
|
|
||||||
...infer Rest extends readonly object[],
|
|
||||||
]
|
|
||||||
? Pick<Self, CommonKeys<Self, Super>> extends Pick<Super, CommonKeys<Self, Super>>
|
|
||||||
? Extendable<readonly [
|
|
||||||
Omit<Super, CommonKeys<Self, Super>> & Self,
|
|
||||||
...Rest,
|
|
||||||
]>
|
|
||||||
: false
|
|
||||||
: true
|
|
||||||
)
|
|
||||||
|
|
||||||
export type NonExtendableKeys<T extends readonly object[]> = (
|
|
||||||
T extends readonly [
|
export type ExtendableFn = ComposeLeft<[
|
||||||
infer Super extends object,
|
ExtendFn,
|
||||||
infer Self extends object,
|
Match<[
|
||||||
...infer Rest extends readonly object[],
|
Match.With<never, false>,
|
||||||
]
|
Match.With<any, true>,
|
||||||
? {[K in keyof Super & keyof Self]: Self[K] extends Super[K]
|
|
||||||
? never
|
|
||||||
: K
|
|
||||||
}[keyof Super & keyof Self]
|
|
||||||
| NonExtendableKeys<readonly [
|
|
||||||
Super & Self,
|
|
||||||
...Rest,
|
|
||||||
]>
|
]>
|
||||||
: void
|
]>
|
||||||
)
|
export type Extendable<T extends object[]> = Call<ExtendableFn, T>
|
||||||
|
|||||||
@@ -1,2 +1,4 @@
|
|||||||
|
export * from "./class"
|
||||||
export * from "./extend"
|
export * from "./extend"
|
||||||
|
export * from "./inheritance"
|
||||||
export * from "./misc"
|
export * from "./misc"
|
||||||
|
|||||||
@@ -1,5 +1,9 @@
|
|||||||
import { CommonKeys } from "."
|
/**
|
||||||
|
* Represents the common keys between two types.
|
||||||
|
* @template A - The first type.
|
||||||
|
* @template B - The second type.
|
||||||
|
*/
|
||||||
|
export type CommonKeys<A, B> = Extract<keyof A, keyof B>
|
||||||
|
|
||||||
/**
|
/**
|
||||||
* Merges an inheritance tree defined by an array of types, considering overrides.
|
* Merges an inheritance tree defined by an array of types, considering overrides.
|
||||||
|
|||||||
@@ -1,17 +1,7 @@
|
|||||||
import { AbstractConstructor } from "type-fest"
|
import { Fn } from "hotscript"
|
||||||
|
import { Simplify } from "type-fest"
|
||||||
|
|
||||||
|
|
||||||
/**
|
export interface SimplifyFn extends Fn {
|
||||||
* Represents the common keys between two types.
|
return: Simplify<this["arg0"]>
|
||||||
* @template A - The first type.
|
}
|
||||||
* @template B - The second type.
|
|
||||||
*/
|
|
||||||
export type CommonKeys<A, B> = Extract<keyof A, keyof B>
|
|
||||||
|
|
||||||
/**
|
|
||||||
* Represents the static members of a class.
|
|
||||||
* @template Class - A class.
|
|
||||||
*/
|
|
||||||
export type StaticMembers<Class extends AbstractConstructor<any>> = Omit<Class, "prototype">
|
|
||||||
|
|
||||||
export function type<T>() { return {} as T }
|
|
||||||
|
|||||||
@@ -8,7 +8,7 @@
|
|||||||
// "allowImportingTsExtensions": true,
|
// "allowImportingTsExtensions": true,
|
||||||
// "noEmit": true,
|
// "noEmit": true,
|
||||||
"declaration": true,
|
"declaration": true,
|
||||||
// "composite": true,
|
"composite": true,
|
||||||
"strict": true,
|
"strict": true,
|
||||||
"downlevelIteration": true,
|
"downlevelIteration": true,
|
||||||
"skipLibCheck": true,
|
"skipLibCheck": true,
|
||||||
|
|||||||
@@ -1,12 +0,0 @@
|
|||||||
import { defineConfig } from "tsup"
|
|
||||||
|
|
||||||
|
|
||||||
export default defineConfig({
|
|
||||||
entry: ["./src/lib.ts", "./src/effect/lib.ts"],
|
|
||||||
format: ["esm", "cjs"],
|
|
||||||
skipNodeModulesBundle: true,
|
|
||||||
dts: true,
|
|
||||||
splitting: true,
|
|
||||||
sourcemap: true,
|
|
||||||
clean: true,
|
|
||||||
})
|
|
||||||
Reference in New Issue
Block a user