Co-authored-by: Julien Valverdé <julien.valverde@mailo.com> Reviewed-on: https://git.jvalver.de/Thilawyn/traitify-ts/pulls/24
This commit was merged in pull request #24.
This commit is contained in:
10
package.json
10
package.json
@@ -1,6 +1,6 @@
|
|||||||
{
|
{
|
||||||
"name": "@thilawyn/traitify-ts",
|
"name": "@thilawyn/traitify-ts",
|
||||||
"version": "0.1.23",
|
"version": "0.1.24",
|
||||||
"type": "module",
|
"type": "module",
|
||||||
"publishConfig": {
|
"publishConfig": {
|
||||||
"registry": "https://git.jvalver.de/api/packages/thilawyn/npm/"
|
"registry": "https://git.jvalver.de/api/packages/thilawyn/npm/"
|
||||||
@@ -30,12 +30,12 @@
|
|||||||
"default": "./dist/effect/lib.cjs"
|
"default": "./dist/effect/lib.cjs"
|
||||||
}
|
}
|
||||||
},
|
},
|
||||||
"./traitsUnique-h-N6REho": {
|
"./traitsUnique-DCJN7XEW": {
|
||||||
"import": {
|
"import": {
|
||||||
"types": "./dist/traitsUnique-h-N6REho.d.ts"
|
"types": "./dist/traitsUnique-DCJN7XEW.d.ts"
|
||||||
},
|
},
|
||||||
"require": {
|
"require": {
|
||||||
"types": "./dist/traitsUnique-h-N6REho.d.ts"
|
"types": "./dist/traitsUnique-DCJN7XEW.d.ts"
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
},
|
},
|
||||||
@@ -55,7 +55,7 @@
|
|||||||
"npm-check-updates": "^16.14.20",
|
"npm-check-updates": "^16.14.20",
|
||||||
"npm-sort": "^0.0.4",
|
"npm-sort": "^0.0.4",
|
||||||
"tsup": "^8.0.2",
|
"tsup": "^8.0.2",
|
||||||
"tsx": "^4.10.0",
|
"tsx": "^4.10.1",
|
||||||
"typescript": "^5.4.5"
|
"typescript": "^5.4.5"
|
||||||
},
|
},
|
||||||
"optionalDependencies": {
|
"optionalDependencies": {
|
||||||
|
|||||||
@@ -1,7 +1,7 @@
|
|||||||
import { AbstractConstructor, Constructor, Simplify } from "type-fest"
|
import { AbstractConstructor, Constructor, Simplify } from "type-fest"
|
||||||
import { Trait } from "./Trait"
|
import { Trait } from "./Trait"
|
||||||
import { TraitExpression } from "./TraitExpression"
|
import { TraitExpression } from "./TraitExpression"
|
||||||
import { Extend, StaticMembers } from "./util"
|
import { Extend, StaticMembers, type } from "./util"
|
||||||
|
|
||||||
|
|
||||||
declare const implSuperSymbol: unique symbol
|
declare const implSuperSymbol: unique symbol
|
||||||
@@ -24,8 +24,19 @@ export class TraitBuilder<
|
|||||||
) {}
|
) {}
|
||||||
|
|
||||||
|
|
||||||
abstract<A extends Abstract>(
|
abstract<A extends AbstractConstructor<Abstract> & StaticAbstract>(
|
||||||
_: (Super: AbstractConstructor<Abstract>) => AbstractConstructor<A, []>
|
_: (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(
|
return new TraitBuilder(
|
||||||
this.traitSuperExpression,
|
this.traitSuperExpression,
|
||||||
@@ -35,8 +46,8 @@ export class TraitBuilder<
|
|||||||
)
|
)
|
||||||
}
|
}
|
||||||
|
|
||||||
staticAbstract<A extends StaticAbstract>(
|
staticAbstractType<A extends StaticAbstract>(
|
||||||
_: (Super: AbstractConstructor<StaticAbstract>) => AbstractConstructor<A, []>
|
_: (Super: StaticAbstract) => typeof type<A>
|
||||||
) {
|
) {
|
||||||
return new TraitBuilder(
|
return new TraitBuilder(
|
||||||
this.traitSuperExpression,
|
this.traitSuperExpression,
|
||||||
@@ -163,7 +174,7 @@ export function implStaticInstantiableThis<
|
|||||||
|
|
||||||
export const trait = new TraitBuilder(
|
export const trait = new TraitBuilder(
|
||||||
new TraitExpression(TraitExpression.NullSuperclass, []),
|
new TraitExpression(TraitExpression.NullSuperclass, []),
|
||||||
{} as object,
|
{} as {},
|
||||||
{} as object,
|
{} as {},
|
||||||
Super => class extends Super {} as AbstractConstructor<object>,
|
Super => class extends Super {} as AbstractConstructor<{}>,
|
||||||
)
|
)
|
||||||
|
|||||||
@@ -142,7 +142,7 @@ export type TraitExpressionClass<
|
|||||||
TraitExpressionStaticMembers<Exp>
|
TraitExpressionStaticMembers<Exp>
|
||||||
)
|
)
|
||||||
|
|
||||||
export type TraitExpressionConcreteClass<
|
export type TraitExpressionInstantiableClass<
|
||||||
Exp extends TraitExpressionLike<any, readonly Trait<any, any, any, any>[]>
|
Exp extends TraitExpressionLike<any, readonly Trait<any, any, any, any>[]>
|
||||||
> = (
|
> = (
|
||||||
Constructor<
|
Constructor<
|
||||||
|
|||||||
@@ -32,17 +32,14 @@ implements TraitExpressionLike<
|
|||||||
|
|
||||||
extends<Self>(): (
|
extends<Self>(): (
|
||||||
AbstractConstructor<
|
AbstractConstructor<
|
||||||
ApplyMutability<S.Struct.Type<Fields>, Mutability> &
|
Simplify<
|
||||||
Omit<
|
ApplyMutability<S.Struct.Type<Fields>, Mutability> &
|
||||||
Simplify<
|
Extend<[
|
||||||
Extend<[
|
Omit<Inherited, keyof Fields>,
|
||||||
Inherited,
|
...TraitTuple.MapImplInstance<Traits>
|
||||||
...TraitTuple.MapImplInstance<Traits>
|
]> &
|
||||||
]>
|
Proto
|
||||||
>,
|
>,
|
||||||
keyof Fields
|
|
||||||
> &
|
|
||||||
Proto,
|
|
||||||
|
|
||||||
ConstructorParameters<S.Class<unknown, Fields, I, R, C, Inherited, Proto>>
|
ConstructorParameters<S.Class<unknown, Fields, I, R, C, Inherited, Proto>>
|
||||||
> &
|
> &
|
||||||
@@ -57,7 +54,6 @@ implements TraitExpressionLike<
|
|||||||
Proto
|
Proto
|
||||||
>
|
>
|
||||||
> &
|
> &
|
||||||
|
|
||||||
Simplify<
|
Simplify<
|
||||||
Extend<[
|
Extend<[
|
||||||
Static,
|
Static,
|
||||||
|
|||||||
@@ -1,4 +1,5 @@
|
|||||||
export { Trait, TraitClass, TraitConcreteClass, TraitInstance, TraitStaticMembers, TraitTuple } from "./Trait"
|
export { Trait, TraitClass, TraitConcreteClass, TraitInstance, TraitStaticMembers, TraitTuple } from "./Trait"
|
||||||
export { ImplStatic, TraitBuilder, implStaticInstantiableThis, implStaticThis, trait } from "./TraitBuilder"
|
export { ImplStatic, TraitBuilder, implStaticInstantiableThis, implStaticThis, trait } from "./TraitBuilder"
|
||||||
export { Implements, StaticImplements, TraitExpression, TraitExpressionClass, TraitExpressionConcreteClass, TraitExpressionInstance, TraitExpressionStaticMembers } from "./TraitExpression"
|
export { Implements, StaticImplements, TraitExpression, TraitExpressionClass, TraitExpressionInstance, TraitExpressionInstantiableClass, TraitExpressionStaticMembers } from "./TraitExpression"
|
||||||
export { TraitExpressionBuilder, expression } from "./TraitExpressionBuilder"
|
export { TraitExpressionBuilder, expression } from "./TraitExpressionBuilder"
|
||||||
|
export { type } from "./util"
|
||||||
|
|||||||
10
src/tests.ts
10
src/tests.ts
@@ -2,6 +2,7 @@ import { TraitClass } from "./Trait"
|
|||||||
import { trait } from "./TraitBuilder"
|
import { trait } from "./TraitBuilder"
|
||||||
import { Implements, StaticImplements, TraitExpressionClass } from "./TraitExpression"
|
import { Implements, StaticImplements, TraitExpressionClass } from "./TraitExpression"
|
||||||
import { expression } from "./TraitExpressionBuilder"
|
import { expression } from "./TraitExpressionBuilder"
|
||||||
|
import { type } from "./util"
|
||||||
|
|
||||||
|
|
||||||
const PrintsHelloOnNew = trait
|
const PrintsHelloOnNew = trait
|
||||||
@@ -18,9 +19,12 @@ const PrintsHelloOnNew = trait
|
|||||||
type PrintsHelloOnNewClass = TraitClass<typeof PrintsHelloOnNew>
|
type PrintsHelloOnNewClass = TraitClass<typeof PrintsHelloOnNew>
|
||||||
|
|
||||||
const Identifiable = <ID>() => trait
|
const Identifiable = <ID>() => trait
|
||||||
.abstract(Super => class extends Super {
|
// .abstract(Super => class extends Super {
|
||||||
declare readonly id: ID
|
// declare readonly id: ID
|
||||||
})
|
// })
|
||||||
|
.abstractType(Super => type<typeof Super & {
|
||||||
|
readonly id: ID
|
||||||
|
}>)
|
||||||
.implement(Super => class Identifiable extends Super {
|
.implement(Super => class Identifiable extends Super {
|
||||||
equals(el: Identifiable) {
|
equals(el: Identifiable) {
|
||||||
return this.id === el.id
|
return this.id === el.id
|
||||||
|
|||||||
@@ -12,6 +12,6 @@ export type CommonKeys<A, B> = Extract<keyof A, keyof B>
|
|||||||
* Represents the static members of a class.
|
* Represents the static members of a class.
|
||||||
* @template Class - A class.
|
* @template Class - A class.
|
||||||
*/
|
*/
|
||||||
export type StaticMembers<Class extends AbstractConstructor<any>> = (
|
export type StaticMembers<Class extends AbstractConstructor<any>> = Omit<Class, "prototype">
|
||||||
Omit<Class, "prototype">
|
|
||||||
)
|
export function type<T>() { return {} as T }
|
||||||
|
|||||||
Reference in New Issue
Block a user