nestjs/docs.nestjs.com

Documentation Issue: Missing explanation for statement

Closed this issue · 2 comments

I'm submitting a...

  • Regression
  • Bug report
  • Feature request
  • Documentation issue or request (new chapter/page)
  • Support request => Please do not submit support request here, instead post your question on Stack Overflow.

Current behavior

> warning **Warning** If your class doesn't extend another class, you should always prefer using **constructor-based** injection.

Why is this?

Expected behavior

The documentation should also explain why this is the case. I've not heard of this before and can't find any other related discussions online.

Minimal reproduction of the problem with instructions

n/a

What is the motivation / use case for changing the behavior?

n/a

Environment

n/a

For Tooling issues:

n/a

Others:

n/a

Agree we should clarify this in the docs. As for your question, there are 2 key reasons: testing is easier & more straightforward, and the constructor explicitly outlines what dependencies are needed to make this class work (while attributes with random @Inject() annotations are not as self-descriptive and easier to miss).

the constructor explicitly outlines what dependencies are needed to make this class work

Makes sense, thanks for clarifying 👍🏻

Small patch at #3018