Skip to content

Commit

Permalink
feat(core): require transform option if second type arg is provided
Browse files Browse the repository at this point in the history
  • Loading branch information
quangtran88 committed Aug 24, 2023
1 parent 22ffa76 commit 7944f6e
Show file tree
Hide file tree
Showing 2 changed files with 20 additions and 1 deletion.
16 changes: 15 additions & 1 deletion packages/core/services/reflector.service.ts
Original file line number Diff line number Diff line change
Expand Up @@ -19,10 +19,18 @@ export interface CreateDecoratorOptions<TParam = any, TTransformed = TParam> {
transform?: (value: TParam) => TTransformed;
}

export type CreateDecoratorWithTransformOptions<
TParam,
TTransformed = TParam,
> = CreateDecoratorOptions<TParam, TTransformed> &
Required<Pick<CreateDecoratorOptions<TParam, TTransformed>, 'transform'>>;

/**
* @publicApi
*/
export type ReflectableDecorator<TParam, TTransformedValue = TParam> = ((opts?: TParam) => CustomDecorator) & {
export type ReflectableDecorator<TParam, TTransformedValue = TParam> = ((
opts?: TParam,
) => CustomDecorator) & {
KEY: string;
};

Expand All @@ -40,6 +48,12 @@ export class Reflector {
* @param options Decorator options.
* @returns A decorator function.
*/
static createDecorator<TParam>(
options?: CreateDecoratorOptions<TParam>,
): ReflectableDecorator<TParam, TParam>;
static createDecorator<TParam, TTransformed>(
options: CreateDecoratorWithTransformOptions<TParam, TTransformed>,
): ReflectableDecorator<TParam, TTransformed>;
static createDecorator<TParam, TTransformed = TParam>(
options: CreateDecoratorOptions<TParam, TTransformed> = {},
): ReflectableDecorator<TParam, TTransformed> {
Expand Down
5 changes: 5 additions & 0 deletions packages/core/test/services/reflector.service.spec.ts
Original file line number Diff line number Diff line change
Expand Up @@ -56,6 +56,11 @@ describe('Reflector', () => {
// @ts-expect-error 'value' is not assignable to type 'number'
reflectedValue = [];
});

it('should require transform option when second generic type is provided', () => {
// @ts-expect-error Property 'transform' is missing in type {} but required in type
const decorator = Reflector.createDecorator<string[], number>({});
});
});

describe('getAll', () => {
Expand Down

0 comments on commit 7944f6e

Please sign in to comment.