nest icon indicating copy to clipboard operation
nest copied to clipboard

feat(common): support empty `@Inject()` on constructor-based injection

Open micalevisk opened this issue 1 year ago • 6 comments
trafficstars

PR Checklist

Please check if your PR fulfills the following requirements:

  • [x] The commit message follows our guidelines: https://github.com/nestjs/nest/blob/master/CONTRIBUTING.md
  • [x] Tests for the changes have been added (for bug fixes / features)
  • [ ] Docs have been added / updated (for bug fixes / features)

PR Type

What kind of change does this PR introduce?

  • [ ] Bugfix
  • [x] Feature
  • [ ] Code style update (formatting, local variables)
  • [ ] Refactoring (no functional changes, no api changes)
  • [ ] Build related changes
  • [ ] CI related changes
  • [ ] Other... Please describe:

What is the current behavior?

Issue Number: closes #13426

What is the new behavior?

We can use @Inject() (ie., no token supplied) on constructor-based injections. It works just like property-based injection

Does this PR introduce a breaking change?

  • [ ] Yes
  • [x] No

micalevisk avatar Apr 13 '24 12:04 micalevisk

Pull Request Test Coverage Report for Build d48faa69-328d-42fb-966e-6301bdeed1a3

Details

  • 4 of 4 (100.0%) changed or added relevant lines in 1 file are covered.
  • No unchanged relevant lines lost coverage.
  • Overall coverage increased (+0.003%) to 92.126%

Totals Coverage Status
Change from base Build 011c0093-57c9-44ca-8bbc-3c112693a9e8: 0.003%
Covered Lines: 6739
Relevant Lines: 7315

💛 - Coveralls

coveralls avatar Apr 13 '24 12:04 coveralls

nestjs will infer the provider token using parameter's metadata. Prior to this, such metadata is always ignored when using Inject on parameters because nestjs always use either the supplied token or the concrete class as the token

I don't know how to explain it more because I didn't follow if you're talking about the transpiled JS code or the runtime behavior.

Here's a video about the reflection on nestjs: https://youtu.be/jo-1EUxMmxc?si=ZvRQ8-6lac6Se3cS&t=779

micalevisk avatar Apr 22 '24 20:04 micalevisk

Sorry, yes i was talking about the runtime behavior.

benjGam avatar Apr 23 '24 20:04 benjGam

regarding that, it will behave just like property-based injection. For them, the @Inject() is used due to introspection limitations. For constructor-based ones, we'll start supporting @Inject() for completeness sake only

micalevisk avatar Apr 23 '24 20:04 micalevisk

Ok, i understand the motivations, thanks for your reply !

benjGam avatar Apr 23 '24 22:04 benjGam

any timeline for this to be merged?

namgk avatar Apr 24 '24 19:04 namgk

lgtm

kamilmysliwiec avatar Jun 03 '24 10:06 kamilmysliwiec