import type { AnyMethod, Getter, Method, MethodKeys, Setter, } from "../../types/concepts"; import type { PropertyConfig } from "../object-operation"; import { overwriteDescriptorConfig } from "../object-operation"; import type { ExtractToMethod } from "./typed"; import { method, property } from "./typed"; export const DefineProperty = ( descriptor: TypedPropertyDescriptor ): PropertyDecorator => property((target, key) => { Object.defineProperty(target, key, descriptor); }); export interface MethodDecoratorContext> { target: This; name: Key; func: ExtractToMethod; } export const WrappedMethod = >( wrapper: Method< This, [ MethodDecoratorContext, ...Parameters> ], ReturnType> >, overwrite?: PropertyConfig ): MethodDecorator => method((target, methodName, descriptor) => { const originalMethod = descriptor.value; // @ts-expect-error Conditional type descriptor.value = function (this: This, ...args: Parameters) { return wrapper.apply(this, [ { name: methodName, target, func: originalMethod! }, ...args, ]); }; overwriteDescriptorConfig(overwrite, descriptor); return descriptor; }); export interface Accessors { get: Getter; set: Setter; } export interface AccessorContext< This, Key extends keyof This, Type extends keyof Accessors > { func: Accessors[Type]; target: This; name: Key; } export const Accesser = < This, Key extends keyof This, Type extends keyof Accessors >( type: Type, handler: Method< This, [ AccessorContext, ...Parameters[Type]> ], ReturnType[Type]> >, overwrite?: PropertyConfig ): MethodDecorator => method( // @ts-expect-error Contravariance (target: This, key: Key, describer: TypedPropertyDescriptor) => { const originalAccessor = describer[type]!; // @ts-expect-error Index type inferred as union type describer[type] = function ( this: This, ...args: Parameters[Type]> ) { return handler.call( this, // @ts-expect-error Index type inferred as union type { func: originalAccessor, name: key, target }, ...args ); }; overwriteDescriptorConfig(overwrite, describer); return describer; } );