Skip to content

Commit dc6dedd

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

File tree

3 files changed

+71
-29
lines changed

3 files changed

+71
-29
lines changed

src/decorators/log-message.decorator.ts

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

77
interface LogMessageDecoratorArgs {
88
message: string;

src/decorators/types.ts

Lines changed: 63 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,63 @@
1+
/**
2+
* The default argument type for decorators, represented as an array with a single unknown type.
3+
*/
4+
export type DefaultDecoratorArgs = [unknown];
5+
6+
/**
7+
* Represents the arguments that a decorator accepts. If the generic type `T` extends an empty tuple,
8+
* the default arguments are `[string, number]`. Otherwise, it uses the provided type `T`.
9+
*/
10+
export type DecoratorArgs<
11+
T extends DefaultDecoratorArgs = DefaultDecoratorArgs,
12+
> = T extends [] ? [string, number] : T;
13+
14+
/**
15+
* Function type definition for a decorator. This function can accept multiple arguments based on `TArgs`
16+
* and has a return type `TReturn` which defaults to `void`.
17+
*/
18+
export type DecoratorFn<TArgs extends DecoratorArgs, TReturn = void> = (
19+
...args: TArgs
20+
) => TReturn;
21+
22+
/**
23+
* Function type definition for a descriptor. This function accepts arguments of type `TArgs`
24+
* and has a return type `TReturn` which defaults to `void`.
25+
*/
26+
export type DescriptorFn<TArgs, TReturn = void> = (args: TArgs) => TReturn;
27+
28+
/**
29+
* Represents a typed property descriptor for an object. The descriptor is associated with
30+
* arguments that the decorator accepts, based on type `TArgs`.
31+
*/
32+
export type Descriptor<
33+
TArgs extends DefaultDecoratorArgs = DefaultDecoratorArgs,
34+
> = TypedPropertyDescriptor<DescriptorValue<TArgs>>;
35+
36+
/**
37+
* Represents a generated decorator. This decorator is applied to a target of type `T`,
38+
* modifies a method or property with key `key`, and takes a descriptor of type `Descriptor<TArgs>`.
39+
*/
40+
export type GeneratedDecorator<
41+
T = unknown,
42+
TArgs extends DefaultDecoratorArgs = DefaultDecoratorArgs,
43+
> = (
44+
target: T,
45+
key: string,
46+
descriptor: Descriptor<TArgs>
47+
) => Descriptor<TArgs>;
48+
49+
/**
50+
* Represents the target context in which a decorator or function is invoked.
51+
* This can be used to bind the `this` value and accept additional decorator arguments.
52+
*/
53+
export type ContextTarget<This, TReturn> = (
54+
this: This,
55+
...args: DecoratorArgs
56+
) => TReturn;
57+
58+
/**
59+
* Represents the value type for a descriptor, which is a function accepting decorator arguments.
60+
*/
61+
export type DescriptorValue<
62+
TArgs extends DefaultDecoratorArgs = DefaultDecoratorArgs,
63+
> = (...args: DecoratorArgs<TArgs>) => any;

src/decorators/utils.ts

Lines changed: 6 additions & 27 deletions
Original file line numberDiff line numberDiff line change
@@ -1,32 +1,11 @@
1+
import {
2+
type ContextTarget,
3+
type DefaultDecoratorArgs,
4+
type DescriptorFn,
5+
type GeneratedDecorator,
6+
} from './types';
17
import logger from '@/lib/logger';
28

3-
export type DefaultDecoratorArgs = [unknown];
4-
export type DecoratorArgs<
5-
T extends DefaultDecoratorArgs = DefaultDecoratorArgs,
6-
> = T extends [] ? [string, number] : T;
7-
export type DecoratorFn<TArgs extends DecoratorArgs, TReturn = void> = (
8-
...args: TArgs
9-
) => TReturn;
10-
export type DescriptorFn<TArgs, TReturn = void> = (args: TArgs) => TReturn;
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-
> = (
18-
target: T,
19-
key: string,
20-
descriptor: Descriptor<TArgs>
21-
) => Descriptor<TArgs>;
22-
export type ContextTarget<This, TReturn> = (
23-
this: This,
24-
...args: DecoratorArgs
25-
) => TReturn;
26-
export type DescriptorValue<
27-
TArgs extends DefaultDecoratorArgs = DefaultDecoratorArgs,
28-
> = (...args: DecoratorArgs<TArgs>) => any;
29-
309
/**
3110
* Creates a decorator that wraps the original method with an additional function.
3211
* @param descriptorFn - The additional function to execute before the original method.

0 commit comments

Comments
 (0)