Repository navigation
Description
🚀 feature request
Relevant Package
This feature request is for @angular/core or @angular/compiler (?)
Description
Angular will currently detect (and complain) if a directive with an input binding exists, but the directive is not known (due to its defining module not being imported):
<app-child [myAwesomeDirective]="42"></app-child>
However, this doesn't work for directives without an input binding, i.e.
<app-child myAwesomeDirective></app-child>
The reason for this is – I believe – that myAwesomeDirective is treated as an HTML attribute (which makes sense), and HTML attributes are, well, legal HTML. And custom attributes are specifically frequently used as content projection selectors.
However, this causes a couple of issues in the developer experience, specifically
- No error being shown if you forget to import the module.
- Making it unsafe to try and identify unused module imports through trial & error of removing them (and there being a lack of alternatives to safely do so).
Describe the solution you'd like
I don't have a specific solution in mind which I think can be considered good enough. Initially I thought about a new strictness setting which fails on unknown HTML attributes, but especially due to the content projection case this would be problematic and a setting I'd only activate for this specific purpose (which feels awkward); in terms of HTML, there's nothing "illegal" about unknown attributes.
I will point out, however, that IDEs such as IntelliJ can and do warn about unknown HTML attributes, even taking content projection into consideration for this. This is somewhat suitable for case (1), though only for code as you write it, not when e.g. moving a component to a different module, and it doesn't cover (2) which is actually my main concern as there's currently no good way of "housekeeping" module imports in a safe, reliable manner. To clarify, this isn't about tree-shaking, unused modules can of course be tree-shaken, but rather about general maintenance and maybe ultimately even allowing to uninstall unused dependencies.
I'm not sure what, if anything, can be reasonably done here, but would like to at least open this for visibility.
To give an example, UI libraries such as Angular Material or ng-zorro-antd require you to import components individually. So you end up importing your buttons, checkboxes, tabs and what have you. But the list only ever grows and can be tough to reduce it down to what is actually needed later on.
Activity
Airblader commentedon Feb 27, 2021
Side note, I am aware that removing a module import could also remove a service provided by it, breaking things, so it will probably never be "safe", unfortunately. However, in practice I didn't find that to be a common issue – unlike directives.
pauldraper commentedon Apr 25, 2021
Would it work to have brackets
[myDirective]refer unambiguously to an Angular input/directive?The issue is just that
[myDirective]currently requires a RHS expression.Airblader commentedon Apr 25, 2021
Do you mean simply allowing
[foo]to work without a RHS such that a compile error is raised if the directive is missing?pauldraper commentedon Apr 26, 2021
Yes.
Airblader commentedon Apr 26, 2021
That would probably be a relatively simple change to at least enable developers to adopt a style that allows catching this kind of thing. It'd still require doing things a certain way by convention, so it'd be helpful, but not really a solution, either.
angular-robot commentedon Jun 4, 2021
Just a heads up that we kicked off a community voting process for your feature request. There are 20 days until the voting process ends.
Find more details about Angular's feature request process in our documentation.
angular-robot commentedon Jun 25, 2021
Thank you for submitting your feature request! Looks like during the polling process it didn't collect a sufficient number of votes to move to the next stage.
We want to keep Angular rich and ergonomic and at the same time be mindful about its scope and learning journey. If you think your request could live outside Angular's scope, we'd encourage you to collaborate with the community on publishing it as an open source package.
You can find more details about the feature request process in our documentation.
JoostK commentedon Sep 13, 2021
Labeling as "under consideration" as I'll be closing #43427 as a duplicate of this one.
alfaproject commentedon Jul 17, 2022
I'm now migrating to standalone directives and actually Angular doesn't seem to complain about missing directives with input binding either anymore. I have to grep my code for the directives and then add the import manually to the new standalone ones ):
robmv commentedon May 21, 2024
Maybe an alternative could be to be able to generate a report at build time of the used directives/components, so they can be analyzed or post processed to identify possible unused dependencies.
s0h311 commentedon May 29, 2024
@JoostK any news about this?
eneajaho commentedon Aug 30, 2024
It's here for standalone components #57605 🎉
JeanMeche commentedon Jan 8, 2026
I think this has been mentionned in other issues. We don't plan to port #57605 for NgModules as it is way trickier to compute what modules are unused (as NgModule also declare providers).
If you want that feature, migrate to standalone (there is a migration to assist you with that))
angular-automatic-lock-bot commentedon Feb 8, 2026
This issue has been automatically locked due to inactivity.
Please file a new issue if you are encountering a similar or related problem.
Read more about our automatic conversation locking policy.
This action has been performed automatically by a bot.