InversifyJS icon indicating copy to clipboard operation
InversifyJS copied to clipboard

Use default class property for @optional injected properties

Open haaijman-imagem opened this issue 3 years ago • 2 comments

Description

Currently the @optional tag overrides class properties defined with typescript-style default values:

@injectable()
class Ninja {
    @inject(Weapon) @optional() weapon: Weapon = new Katana();

    ....
}

this currently sets the weapon property to undefined if no Weapon is present in the container. My proposed change allows does not override the default and results in the weapon property being set to new Katana().

(Default values in the constructor already work, but I would prefer not to add boilerplate constructors if this solution is possible.)

Related Issue

@optional property injection overrides default

Motivation and Context

How Has This Been Tested?

By copying the test case for optional constructor injection but moving the injection to property injection and then running npm test

Types of changes

  • [x] Updated docs / Refactor code / Added a tests case (non-breaking change)
  • [ ] Bug fix (non-breaking change which fixes an issue)
  • [x] New feature (non-breaking change which adds functionality)
  • [ ] Breaking change (fix or feature that would cause existing functionality to change)

Checklist:

  • [x] My code follows the code style of this project.
  • [x] My change requires a change to the documentation.
  • [x] I have updated the documentation accordingly.
  • [x] I have read the CONTRIBUTING document.
  • [x] I have added tests to cover my changes.
  • [x] All new and existing tests passed.
  • [x] I have updated the changelog.

haaijman-imagem avatar Jun 23 '22 06:06 haaijman-imagem

Codecov Report

Merging #1467 (56055bf) into master (758a6bd) will increase coverage by 0.05%. The diff coverage is 100.00%.

:exclamation: Your organization needs to install the Codecov GitHub app to enable full functionality.

@@            Coverage Diff             @@
##           master    #1467      +/-   ##
==========================================
+ Coverage   99.56%   99.61%   +0.05%     
==========================================
  Files          53       53              
  Lines        1364     1568     +204     
  Branches      196      206      +10     
==========================================
+ Hits         1358     1562     +204     
  Partials        6        6              
Files Coverage Δ
src/resolution/instantiation.ts 96.72% <100.00%> (+0.05%) :arrow_up:

... and 22 files with indirect coverage changes

:mega: We’re building smart automated test selection to slash your CI/CD build times. Learn more

codecov-commenter avatar Aug 13 '22 09:08 codecov-commenter

LGTM. can I have your opinion here as well @jakehamtexas ?

PodaruDragos avatar Aug 27 '22 15:08 PodaruDragos

LGTM. can I have your opinion here as well @jakehamtexas ?

Any updates on this?

haaijman-imagem avatar Mar 08 '23 12:03 haaijman-imagem

@haaijman-imagem can you update the branch? After that I would say we can merge it

notaphplover avatar Mar 09 '23 10:03 notaphplover