nest
nest copied to clipboard
refactor(common,core): support read-only arrays on module metadata
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
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.
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 | |
|---|---|
| Change from base Build 0a70a393-e94d-4204-995d-b0e19a5c9480: | 0.0% |
| Covered Lines: | 6744 |
| Relevant Lines: | 7314 |
💛 - 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
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?
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
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)