Skip to content

Commit 2341c09

Browse files
committed
refactor(decorator): fix types
1 parent eb28dda commit 2341c09

File tree

1 file changed

+21
-16
lines changed

1 file changed

+21
-16
lines changed

src/decorators/utils.ts

Lines changed: 21 additions & 16 deletions
Original file line numberDiff line numberDiff line change
@@ -3,20 +3,29 @@ import logger from '@/lib/logger';
33
export type DefaultDecoratorArgs = [unknown];
44
export type DecoratorArgs<
55
T extends DefaultDecoratorArgs = DefaultDecoratorArgs,
6-
> = T extends DefaultDecoratorArgs ? [string, number] : T;
6+
> = T extends [] ? [string, number] : T;
77
export type DecoratorFn<TArgs extends DecoratorArgs, TReturn = void> = (
88
...args: TArgs
99
) => TReturn;
1010
export type DescriptorFn<TArgs, TReturn = void> = (args: TArgs) => TReturn;
11-
export type GeneratedDecorator<T = unknown> = (
11+
export type Descriptor<
12+
TArgs extends DefaultDecoratorArgs = DefaultDecoratorArgs,
13+
> = TypedPropertyDescriptor<DescriptorValue<TArgs>>;
14+
export type GeneratedDecorator<
15+
T = unknown,
16+
TArgs extends DefaultDecoratorArgs = DefaultDecoratorArgs,
17+
> = (
1218
target: T,
1319
key: string,
14-
descriptor: PropertyDescriptor
15-
) => PropertyDescriptor;
20+
descriptor: Descriptor<TArgs>
21+
) => Descriptor<TArgs>;
1622
export type ContextTarget<This, TReturn> = (
1723
this: This,
1824
...args: DecoratorArgs
1925
) => TReturn;
26+
export type DescriptorValue<
27+
TArgs extends DefaultDecoratorArgs = DefaultDecoratorArgs,
28+
> = (...args: DecoratorArgs<TArgs>) => any;
2029

2130
/**
2231
* Creates a decorator that wraps the original method with an additional function.
@@ -31,14 +40,14 @@ export function createDecorator<
3140
>(
3241
descriptorFn: DescriptorFn<TFnArgs, TReturn>,
3342
descriptorArgs: TFnArgs
34-
): GeneratedDecorator<any> {
35-
return function (_target: any, key: string, descriptor: PropertyDescriptor) {
43+
): GeneratedDecorator<unknown, TArgs> {
44+
return function (_target, key, descriptor) {
3645
const originalMethod = descriptor.value;
37-
descriptor.value = async function (...args: DecoratorArgs<TArgs>) {
46+
descriptor.value = async function (...args) {
3847
logger.info(`Executing decorator before method: ${key}`);
3948
descriptorFn(descriptorArgs);
4049
logger.info('Decorator executed');
41-
return originalMethod.apply(this, args);
50+
return originalMethod ? originalMethod.apply(this, args) : null;
4251
};
4352
return descriptor;
4453
};
@@ -62,18 +71,14 @@ export function createContextDecorator<
6271
_target: ContextTarget<This, TReturn>,
6372
descriptorFn: DescriptorFn<TFnArgs, TReturn>,
6473
descriptorArgs: TFnArgs
65-
): GeneratedDecorator<ContextTarget<This, TReturn>> {
74+
): GeneratedDecorator<ContextTarget<This, TReturn>, TArgs> {
6675
const methodName = String(context.name);
6776
logger.info(`Executing decorator for ${methodName}`);
68-
return function (
69-
_target: ContextTarget<This, TReturn>,
70-
_key: string,
71-
descriptor: PropertyDescriptor
72-
) {
77+
return function (_target, _key, descriptor) {
7378
const originalMethod = descriptor.value;
74-
descriptor.value = async function (...args: DecoratorArgs<TArgs>) {
79+
descriptor.value = async function (...args) {
7580
descriptorFn(descriptorArgs);
76-
return originalMethod.apply(this, args);
81+
return originalMethod ? originalMethod.apply(this, args) : null;
7782
};
7883
return descriptor;
7984
};

0 commit comments

Comments
 (0)