Skip to content

Commit eb28dda

Browse files
committed
refactor(decorator): fix types
1 parent 1196dff commit eb28dda

File tree

3 files changed

+24
-6
lines changed

3 files changed

+24
-6
lines changed

src/decorators/log-message.decorator.ts

Lines changed: 8 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,8 @@
1-
import { type DescriptorFn, createDecorator } from '@/decorators/utils';
1+
import {
2+
type DescriptorFn,
3+
createDecorator,
4+
type DefaultDecoratorArgs,
5+
} from '@/decorators/utils';
26

37
interface LogMessageDecoratorArgs {
48
message: string;
@@ -14,7 +18,9 @@ const descriptorFn: LogMessageDescriptor = ({ message }) => {
1418
* This is a sample decorator created using the `createDecorator()` method.
1519
* The same format can be used to create your own custom decorators.
1620
*/
17-
const LogMessage = <TArgs = unknown>(args: LogMessageDecoratorArgs) => {
21+
const LogMessage = <TArgs extends DefaultDecoratorArgs = DefaultDecoratorArgs>(
22+
args: LogMessageDecoratorArgs
23+
) => {
1824
return createDecorator<
1925
LogMessageDecoratorArgs,
2026
TArgs,

src/decorators/utils.ts

Lines changed: 15 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,9 @@
11
import logger from '@/lib/logger';
22

3-
export type DecoratorArgs<T = unknown> = [T, string, number];
3+
export type DefaultDecoratorArgs = [unknown];
4+
export type DecoratorArgs<
5+
T extends DefaultDecoratorArgs = DefaultDecoratorArgs,
6+
> = T extends DefaultDecoratorArgs ? [string, number] : T;
47
export type DecoratorFn<TArgs extends DecoratorArgs, TReturn = void> = (
58
...args: TArgs
69
) => TReturn;
@@ -21,7 +24,11 @@ export type ContextTarget<This, TReturn> = (
2124
* @param descriptorArgs - Arguments for the descriptor function.
2225
* @returns The generated decorator function.
2326
*/
24-
export function createDecorator<TFnArgs, TArgs = unknown, TReturn = void>(
27+
export function createDecorator<
28+
TFnArgs,
29+
TArgs extends DefaultDecoratorArgs = DefaultDecoratorArgs,
30+
TReturn = void,
31+
>(
2532
descriptorFn: DescriptorFn<TFnArgs, TReturn>,
2633
descriptorArgs: TFnArgs
2734
): GeneratedDecorator<any> {
@@ -45,7 +52,12 @@ export function createDecorator<TFnArgs, TArgs = unknown, TReturn = void>(
4552
* @param descriptorArgs - Arguments for the descriptor function.
4653
* @returns The generated context-aware decorator.
4754
*/
48-
export function createContextDecorator<This, TReturn, TFnArgs, TArgs = unknown>(
55+
export function createContextDecorator<
56+
This,
57+
TReturn,
58+
TFnArgs,
59+
TArgs extends DefaultDecoratorArgs = DefaultDecoratorArgs,
60+
>(
4961
context: ClassMethodDecoratorContext<This, ContextTarget<This, TReturn>>,
5062
_target: ContextTarget<This, TReturn>,
5163
descriptorFn: DescriptorFn<TFnArgs, TReturn>,

src/modules/users/users.service.ts

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -3,7 +3,7 @@ import prisma from '@/lib/prisma';
33
import LogMessage from '@/decorators/log-message.decorator';
44

55
export default class UserService {
6-
@LogMessage({ message: 'test-decorator' })
6+
@LogMessage<[users]>({ message: 'test-decorator' })
77
public async createUser(data: users) {
88
const user = await prisma.users.create({ data });
99
return user;

0 commit comments

Comments
 (0)