|
| 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; |
0 commit comments