0.1.21 #21
28
package.json
28
package.json
@@ -1,6 +1,6 @@
|
||||
{
|
||||
"name": "@thilawyn/traitify-ts",
|
||||
"version": "0.1.20",
|
||||
"version": "0.1.21",
|
||||
"type": "module",
|
||||
"publishConfig": {
|
||||
"registry": "https://git.jvalver.de/api/packages/thilawyn/npm/"
|
||||
@@ -19,6 +19,24 @@
|
||||
"types": "./dist/lib.d.cts",
|
||||
"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-CJ9oW-Fc": {
|
||||
"import": {
|
||||
"types": "./dist/traitsUnique-CJ9oW-Fc.d.ts"
|
||||
},
|
||||
"require": {
|
||||
"types": "./dist/traitsUnique-CJ9oW-Fc.d.ts"
|
||||
}
|
||||
}
|
||||
},
|
||||
"scripts": {
|
||||
@@ -30,18 +48,18 @@
|
||||
},
|
||||
"dependencies": {
|
||||
"remeda": "^1.61.0",
|
||||
"type-fest": "^4.18.0"
|
||||
"type-fest": "^4.18.2"
|
||||
},
|
||||
"devDependencies": {
|
||||
"bun-types": "latest",
|
||||
"npm-check-updates": "^16.14.20",
|
||||
"npm-sort": "^0.0.4",
|
||||
"tsup": "^8.0.2",
|
||||
"tsx": "^4.7.3",
|
||||
"tsx": "^4.10.0",
|
||||
"typescript": "^5.4.5"
|
||||
},
|
||||
"optionalDependencies": {
|
||||
"@effect/schema": "^0.66.12",
|
||||
"effect": "^3.1.0"
|
||||
"@effect/schema": "^0.67.0",
|
||||
"effect": "^3.1.3"
|
||||
}
|
||||
}
|
||||
|
||||
@@ -4,10 +4,20 @@ import { TraitBuilder } from "./TraitBuilder"
|
||||
import { Extend, StaticMembers } from "./util"
|
||||
|
||||
|
||||
export type TraitExpressionLike<
|
||||
Superclass extends AbstractClass<object>,
|
||||
Traits extends readonly Trait<any, any, any, any>[],
|
||||
> = {
|
||||
readonly superclass: Superclass,
|
||||
readonly traits: Traits,
|
||||
}
|
||||
|
||||
|
||||
export class TraitExpression<
|
||||
Superclass extends AbstractClass<object>,
|
||||
const Traits extends readonly Trait<any, any, any, any>[],
|
||||
> {
|
||||
>
|
||||
implements TraitExpressionLike<Superclass, Traits> {
|
||||
constructor(
|
||||
readonly superclass: Superclass,
|
||||
readonly traits: Traits,
|
||||
@@ -84,13 +94,13 @@ export namespace TraitExpression {
|
||||
}
|
||||
|
||||
export type Superclass<T> = (
|
||||
T extends TraitExpression<infer Superclass, any>
|
||||
T extends TraitExpressionLike<infer Superclass, any>
|
||||
? Superclass
|
||||
: never
|
||||
)
|
||||
|
||||
export type Traits<T> = (
|
||||
T extends TraitExpression<any, infer Traits>
|
||||
T extends TraitExpressionLike<any, infer Traits>
|
||||
? Traits
|
||||
: never
|
||||
)
|
||||
@@ -98,7 +108,7 @@ export namespace TraitExpression {
|
||||
|
||||
|
||||
export type Implements<
|
||||
Exp extends TraitExpression<any, readonly Trait<any, any, any, any>[]>
|
||||
Exp extends TraitExpressionLike<any, readonly Trait<any, any, any, any>[]>
|
||||
> = (
|
||||
Simplify<
|
||||
Extend<
|
||||
@@ -110,7 +120,7 @@ export type Implements<
|
||||
)
|
||||
|
||||
export type StaticImplements<
|
||||
Exp extends TraitExpression<any, readonly Trait<any, any, any, any>[]>
|
||||
Exp extends TraitExpressionLike<any, readonly Trait<any, any, any, any>[]>
|
||||
> = (
|
||||
Simplify<
|
||||
Extend<
|
||||
@@ -123,7 +133,7 @@ export type StaticImplements<
|
||||
|
||||
|
||||
export type TraitExpressionClass<
|
||||
Exp extends TraitExpression<any, readonly Trait<any, any, any, any>[]>
|
||||
Exp extends TraitExpressionLike<any, readonly Trait<any, any, any, any>[]>
|
||||
> = (
|
||||
AbstractClass<
|
||||
TraitExpressionInstance<Exp>,
|
||||
@@ -133,7 +143,7 @@ export type TraitExpressionClass<
|
||||
)
|
||||
|
||||
export type TraitExpressionConcreteClass<
|
||||
Exp extends TraitExpression<any, readonly Trait<any, any, any, any>[]>
|
||||
Exp extends TraitExpressionLike<any, readonly Trait<any, any, any, any>[]>
|
||||
> = (
|
||||
Class<
|
||||
TraitExpressionInstance<Exp>,
|
||||
@@ -143,7 +153,7 @@ export type TraitExpressionConcreteClass<
|
||||
)
|
||||
|
||||
export type TraitExpressionInstance<
|
||||
Exp extends TraitExpression<any, readonly Trait<any, any, any, any>[]>
|
||||
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
|
||||
@@ -155,7 +165,7 @@ export type TraitExpressionInstance<
|
||||
)
|
||||
|
||||
export type TraitExpressionStaticMembers<
|
||||
Exp extends TraitExpression<any, readonly Trait<any, any, any, any>[]>
|
||||
Exp extends TraitExpressionLike<any, readonly Trait<any, any, any, any>[]>
|
||||
> = (
|
||||
Simplify<
|
||||
Extend<[
|
||||
|
||||
@@ -1,7 +1,8 @@
|
||||
import { unique } from "remeda"
|
||||
import { AbstractClass, IsEqual } from "type-fest"
|
||||
import { AbstractClass } from "type-fest"
|
||||
import { Trait, TraitTuple } from "./Trait"
|
||||
import { TraitExpression } from "./TraitExpression"
|
||||
import { SpreadSupertraits, spreadSupertraits } from "./spreadSupertraits"
|
||||
import { TraitsUnique, traitsUnique } from "./traitsUnique"
|
||||
import { Extendable, StaticMembers } from "./util"
|
||||
|
||||
|
||||
@@ -9,56 +10,18 @@ export class TraitExpressionBuilder<
|
||||
Superclass extends AbstractClass<object>,
|
||||
const Traits extends readonly Trait<any, any, any, any>[],
|
||||
> {
|
||||
declare ["constructor"]: typeof TraitExpressionBuilder
|
||||
|
||||
constructor(
|
||||
readonly expressionSuperclass: Superclass,
|
||||
readonly expressionTraits: Traits,
|
||||
) {}
|
||||
|
||||
|
||||
static 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 TraitExpressionBuilder.SpreadSupertraits<T>
|
||||
}
|
||||
|
||||
static 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 TraitExpressionBuilder.TraitsUnique<T>
|
||||
}
|
||||
|
||||
|
||||
extends<
|
||||
Super extends AbstractClass<object>
|
||||
>(
|
||||
superclass: Super
|
||||
) {
|
||||
return new this.constructor(
|
||||
return new TraitExpressionBuilder(
|
||||
superclass,
|
||||
this.expressionTraits,
|
||||
)
|
||||
@@ -80,12 +43,12 @@ export class TraitExpressionBuilder<
|
||||
Superclass,
|
||||
TraitExpressionBuilder.ExpressesReturnTypeTraits<Traits, T>
|
||||
> {
|
||||
return new this.constructor(
|
||||
return new TraitExpressionBuilder(
|
||||
this.expressionSuperclass,
|
||||
|
||||
this.constructor.traitsUnique([
|
||||
traitsUnique([
|
||||
...this.expressionTraits,
|
||||
...this.constructor.spreadSupertraits(traits),
|
||||
...spreadSupertraits(traits),
|
||||
]),
|
||||
)
|
||||
}
|
||||
@@ -106,11 +69,11 @@ export class TraitExpressionBuilder<
|
||||
Superclass,
|
||||
TraitExpressionBuilder.ExpressesFirstReturnTypeTraits<Traits, T>
|
||||
> {
|
||||
return new this.constructor(
|
||||
return new TraitExpressionBuilder(
|
||||
this.expressionSuperclass,
|
||||
|
||||
this.constructor.traitsUnique([
|
||||
...this.constructor.spreadSupertraits(traits),
|
||||
traitsUnique([
|
||||
...spreadSupertraits(traits),
|
||||
...this.expressionTraits,
|
||||
]),
|
||||
)
|
||||
@@ -141,44 +104,13 @@ export class TraitExpressionBuilder<
|
||||
}
|
||||
|
||||
export namespace TraitExpressionBuilder {
|
||||
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 []
|
||||
)
|
||||
|
||||
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
|
||||
)
|
||||
|
||||
export type ExpressesReturnTypeTraits<
|
||||
Traits extends readonly Trait<any, any, any, any>[],
|
||||
T extends readonly Trait<any, any, any, any>[],
|
||||
> = (
|
||||
TraitExpressionBuilder.TraitsUnique<readonly [
|
||||
TraitsUnique<readonly [
|
||||
...Traits,
|
||||
...TraitExpressionBuilder.SpreadSupertraits<T>,
|
||||
...SpreadSupertraits<T>,
|
||||
]>
|
||||
)
|
||||
|
||||
@@ -186,8 +118,8 @@ export namespace TraitExpressionBuilder {
|
||||
Traits extends readonly Trait<any, any, any, any>[],
|
||||
T extends readonly Trait<any, any, any, any>[],
|
||||
> = (
|
||||
TraitExpressionBuilder.TraitsUnique<readonly [
|
||||
...TraitExpressionBuilder.SpreadSupertraits<T>,
|
||||
TraitsUnique<readonly [
|
||||
...SpreadSupertraits<T>,
|
||||
...Traits,
|
||||
]>
|
||||
)
|
||||
|
||||
@@ -1,39 +0,0 @@
|
||||
import { Schema as S } from "@effect/schema"
|
||||
import { Simplify } from "type-fest"
|
||||
import { Trait, TraitTuple } from "./Trait"
|
||||
import { TraitExpression } from "./TraitExpression"
|
||||
import { Extend } from "./util"
|
||||
|
||||
|
||||
export function extendsEffectSchemaExpression<
|
||||
Fields extends S.Struct.Fields,
|
||||
A, I, R, C,
|
||||
Inherited extends object,
|
||||
Proto,
|
||||
|
||||
const Traits extends readonly Trait<any, any, any, any>[],
|
||||
>(
|
||||
expression: TraitExpression<
|
||||
S.Class<unknown, Fields, A, I, R, C, Inherited, Proto>,
|
||||
Traits
|
||||
>
|
||||
) {
|
||||
return <Self>(): (
|
||||
S.Class<
|
||||
Self,
|
||||
Fields,
|
||||
A, I, R, C,
|
||||
Simplify<
|
||||
Extend<[
|
||||
Inherited,
|
||||
...TraitTuple.MapImplInstance<Traits>
|
||||
]>
|
||||
>,
|
||||
Proto
|
||||
> &
|
||||
|
||||
Simplify<
|
||||
Extend<TraitTuple.MapImplStaticMembers<Traits>>
|
||||
>
|
||||
) => expression.extends as any
|
||||
}
|
||||
84
src/effect/EffectSchemaTraitExpression.ts
Normal file
84
src/effect/EffectSchemaTraitExpression.ts
Normal file
@@ -0,0 +1,84 @@
|
||||
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<
|
||||
ApplyMutability<S.Struct.Type<Fields>, Mutability> &
|
||||
Omit<Inherited, keyof Fields> &
|
||||
Proto,
|
||||
|
||||
ConstructorParameters<S.Class<unknown, Fields, I, R, C, Inherited, Proto>>
|
||||
> &
|
||||
|
||||
StaticMembers<
|
||||
S.Class<
|
||||
Self,
|
||||
Fields,
|
||||
ApplyMutability<I, EncodedMutability>,
|
||||
R, C,
|
||||
Simplify<
|
||||
Extend<[
|
||||
Inherited,
|
||||
...TraitTuple.MapImplInstance<Traits>
|
||||
]>
|
||||
>,
|
||||
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] }
|
||||
)
|
||||
206
src/effect/EffectSchemaTraitExpressionBuilder.ts
Normal file
206
src/effect/EffectSchemaTraitExpressionBuilder.ts
Normal file
@@ -0,0 +1,206 @@
|
||||
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()
|
||||
2
src/effect/lib.ts
Normal file
2
src/effect/lib.ts
Normal file
@@ -0,0 +1,2 @@
|
||||
export { EffectSchemaTraitExpression } from "./EffectSchemaTraitExpression"
|
||||
export { EffectSchemaInitialTraitExpressionBuilder, EffectSchemaTraitExpressionBuilder, effectSchemaExpression } from "./EffectSchemaTraitExpressionBuilder"
|
||||
49
src/effect/tests.ts
Normal file
49
src/effect/tests.ts
Normal file
@@ -0,0 +1,49 @@
|
||||
import { Schema as S } from "@effect/schema"
|
||||
import { Implements } from "../TraitExpression"
|
||||
import { EffectSchemaTraitExpression } from "./EffectSchemaTraitExpression"
|
||||
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"
|
||||
@@ -2,4 +2,3 @@ export { Trait, TraitClass, TraitConcreteClass, TraitInstance, TraitStaticMember
|
||||
export { ImplStatic, TraitBuilder, implStaticInstantiableThis, implStaticThis, trait } from "./TraitBuilder"
|
||||
export { Implements, StaticImplements, TraitExpression, TraitExpressionClass, TraitExpressionConcreteClass, TraitExpressionInstance, TraitExpressionStaticMembers } from "./TraitExpression"
|
||||
export { TraitExpressionBuilder, expression } from "./TraitExpressionBuilder"
|
||||
export { extendsEffectSchemaExpression } from "./effect"
|
||||
|
||||
35
src/spreadSupertraits.ts
Normal file
35
src/spreadSupertraits.ts
Normal file
@@ -0,0 +1,35 @@
|
||||
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 []
|
||||
)
|
||||
39
src/traitsUnique.ts
Normal file
39
src/traitsUnique.ts
Normal file
@@ -0,0 +1,39 @@
|
||||
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
|
||||
)
|
||||
@@ -2,10 +2,11 @@ import { defineConfig } from "tsup"
|
||||
|
||||
|
||||
export default defineConfig({
|
||||
entry: ["./src/lib.ts"],
|
||||
entry: ["./src/lib.ts", "./src/effect/lib.ts"],
|
||||
format: ["esm", "cjs"],
|
||||
skipNodeModulesBundle: true,
|
||||
dts: true,
|
||||
splitting: false,
|
||||
splitting: true,
|
||||
sourcemap: true,
|
||||
clean: true,
|
||||
})
|
||||
|
||||
Reference in New Issue
Block a user