Under The Hood
backend nestjs NestJS 10+

createParamDecorator(): Custom Parameter Decorators & Metadata Extraction

Last updated
Prerequisites:
NestJS ExecutionContext
TypeScript Parameter Decorators
  • nestjs
  • decorators
  • createParamDecorator
  • param-extraction
  • clean-code

Read at your depth

The practical view

`createParamDecorator()` creates custom parameter decorators in NestJS. `export const User = createParamDecorator((data: string, ctx: ExecutionContext) => { const req = ctx.switchToHttp().getRequest(); return data ? req.user?.[data] : req.user; })`. In your controller, you can write `@Get() getProfile(@User() user: UserEntity, @User('id') userId: string)`.

The same idea in other frameworks

nestjs equivalent
`export const User = createParamDecorator((data, ctx) => ...)`.

Legacy vs modern

Manual @Req() Inspection vs Declarative @User() Param Decorator

Extracting properties manually from `@Req()` couples controllers to Express; `@User()` creates clean, typed parameters.

before → after
Manual @Req() Inspection
getProfile(@Req() req: any) { const user = req.user; // ❌ Directly coupled to Express
Custom @User() Decorator
getProfile(@User('id', ParseIntPipe) userId: number) { // ✅ NEW: Clean, typed, pipeable

Interview gotchas

Context

Interviewers test your knowledge of parameter evaluation pipelines in NestJS.

The mechanical answer

Yes. Custom parameter decorators integrate directly into the NestJS Pipe pipeline: `@Get() findOne(@User('id', ParseUUIDPipe) userId: string)`. NestJS first extracts the value using your custom decorator function, and then passes the extracted return value directly into `ParseUUIDPipe` for validation before the controller method runs.

Trap

The pipe receives the *extracted* value from the decorator, not the full request object.

Under The Hood — a multi-depth technical interview hub.

Third-party licenses Press ⌘ K to search.