NestJS · Middle · Question 9 of 75

How do you write a custom parameter decorator like @User()?

export const User = createParamDecorator((data: unknown, ctx: ExecutionContext) => {
  return ctx.switchToHttp().getRequest().user;
});

Explanation

Custom decorators can also accept data (@User('email')) and be combined with pipes, e.g. @User(new ValidationPipe()).

Further reading

Problem with a question

NestJS · Middle · question 9

Test yourself

Take the NestJS · Middle quiz: questions and options are shuffled, and you get a review of your mistakes at the end.

Take this quiz