nestjs / nest

A progressive Node.js framework for building efficient, scalable, and enterprise-grade server-side applications with TypeScript/JavaScript 🚀
https://nestjs.com
MIT License
67.74k stars 7.63k forks source link

Can't use interceptor with optional parameter as type #9544

Closed superpupervlad closed 2 years ago

superpupervlad commented 2 years ago

Is there an existing issue for this?

Current behavior

@UseInterceptors can't handle interceptor (with optional parameters) that passed as type.

Minimum reproduction code

https://stackblitz.com/edit/nestjs-typescript-starter-bnqkzn?file=src/app.controller.ts

Steps to reproduce

No response

Expected behavior

Due to documentation (leaving responsibility for instantiation to the framework and enabling dependency injection) nest should create an instance of interceptor because no parameters needed from user and it is possible to create interceptor.

Package

Other package

No response

NestJS version

8.1.1

Packages versions

From stackblitz

[Nest CLI]
Nest CLI Version : 8.1.3 

[Nest Platform Information]
platform-express version : 8.1.1
schematics version       : 8.0.4
testing version          : 8.1.1
common version           : 8.1.1
core version             : 8.1.1
cli version              : 8.1.3

Node.js version

16.14.2

In which operating systems have you tested?

Other

No response

micalevisk commented 2 years ago

your interceptor should have this constructor:

  constructor(
    @Optional() private first_optional_param?: string,
    @Optional() private second_optional_param: string = 'abc',
  ) {}

learn here: https://docs.nestjs.com/providers#optional-providers

jmcdo29 commented 2 years ago

This is not a bug. Micael has the correct solution above.

For further support, please use our Discord channel (Support). We are using GitHub to track Bug Reports, Feature Requests, and Regressions.