nest icon indicating copy to clipboard operation
nest copied to clipboard

refactor(common,core): support read-only arrays on module metadata

Open dfanara opened this issue 1 year ago • 5 comments
trafficstars

Add support for ModuleMetadata to accept ReadOnlyArray. This allows developers to use TypeScript const assertions for module definitions.

There are no breaking changes with this commit and this change is discussed in #13326.

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)
  • [x] Docs have been added / updated (for bug fixes / features)

PR Type

What kind of change does this PR introduce?

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

What is the current behavior?

Issue Number: #13326

What is the new behavior?

ReadOnlyArray is not supported to better support using TypeScript const assertions in module definitions.

Does this PR introduce a breaking change?

  • [ ] Yes
  • [x] No

Other information

dfanara avatar Jun 06 '24 23:06 dfanara

Are there additional test cases you would like to see? I added a test case for NestContainer since that was a direct change to the function definition, however, I wasn't sure if a test was necessary for the ModuleMetadata interface changes.

dfanara avatar Jun 06 '24 23:06 dfanara

Pull Request Test Coverage Report for Build f9bc21b9-dc31-40b9-a29a-6e907ff79d17

Details

  • 0 of 0 changed or added relevant lines in 0 files are covered.
  • No unchanged relevant lines lost coverage.
  • Overall coverage remained the same at 92.207%

Totals Coverage Status
Change from base Build 0a70a393-e94d-4204-995d-b0e19a5c9480: 0.0%
Covered Lines: 6744
Relevant Lines: 7314

💛 - Coveralls

coveralls avatar Jun 06 '24 23:06 coveralls

I just noticed something that would break due to that ReadonlyArray:

const imports: ModuleMetadata['imports'] = [];
imports.push(class {})

because we can't invoke .push on readonly arrays

and I believe that we shouldn't prevent that usage

micalevisk avatar Jun 09 '24 17:06 micalevisk

Ah yeah, I wasn't sure about that. Should something like the nest container be able to modify an input array in that use case? I'm not too familiar with the internals (thought this might be a good first issue to really start learning more about that), but in general it seems like pushing an item onto that array might cause some unintended side effects?

dfanara avatar Jun 12 '24 01:06 dfanara

but in general it seems like pushing an item onto that array might cause some unintended side effects?

I don't think so

But I was talking about a valid client-land use case. Imagine that you want to define the imports array dynamically in a dynamic module, and you can leverage on that type definition to properly type your array

micalevisk avatar Jun 12 '24 02:06 micalevisk

as @micalevisk pointed out above, we sometimes need to dynamically update the module's metadata (create an object first and then fill it with refs)

kamilmysliwiec avatar Aug 12 '24 08:08 kamilmysliwiec