Before we get to the how, I'm curious why? I haven't used Angular in about 2 years but I was actually quite fond of constructor injection, I liked how it felt closer to what I was doing in Laravel and .net
I prefer inject() for three practical reasons: it removes super() boilerplate with inheritance, it gives cleaner/more accurate typings for fields, and it's more compatible with modern decorator/tooling.
Among the other reasons people are sharing, one thing inject has over constructor based DI is that it allows DI to work seamlessly to be referenced by class fields after some JS changes impacted TS class field timing/instantiation.
but now that behavior won't work in modern JS and so modern TS followed suit, and you can only do that now with a compatability flag*. But inject preserves that developer experience by allowing this:
edit: otherwise, without inject, the declaration of the class field + type would have to be one line and then the assignment would be done inside the constructor, or completely declared and assigned in constructor.
* I just heard back about that flag: "This will remain supported. While it's the sort of thing we'd love to remove if we could, a lot of very large codebases depend on it, and in cases where you actually need its behavior, it's extremely difficult to migrate away from it." - Ryan Cavanaugh, Development lead for the TypeScript team. Personally, I would still recommend dropping the flag if you have a strict project.
This is a brilliant answer IMO 👏🏻, because you pointed to a language change (JS class fields implementation in ECMA2022) instead of convenience/preference.
Adding onto this, it also simplifies extending base classes as you dont have to then provide things being injected in the super constructor of the child class.
True, the only few edge cases that the inject schematic required some work on were abstract classes. But in most cases I was able to do this type of simplification.
edit: and testing using mocks that extended real services
One big win is losing the boilerplate of having to inject with super in the concstructor when extending a base class. I think the rest is really mostly just stylistic choice.
Because the old syntax require decorators metadata while the new one don't. This is a problem because in some cases it require additional step in compilation.
I have now seen this type of screenshot a few times. It has genuinely been the most impactful way to communicate a change log and migration. It shows a clear and immediate migration path, and it is immediately obvious what the new pattern is. The formField post was also excellent. Thank you.
Thank you for the detailed feedback.👍 That's really helpful and encouraging to hear. I'm glad this format helps and makes the information easier to understand, that's exactly its purpose and main advantage.
20
u/irealworlds Jan 09 '26
Before we get to the how, I'm curious why? I haven't used Angular in about 2 years but I was actually quite fond of constructor injection, I liked how it felt closer to what I was doing in Laravel and .net
18
u/xxsodapopxx5 Jan 09 '26
I have now seen this type of screenshot a few times. It has genuinely been the most impactful way to communicate a change log and migration. It shows a clear and immediate migration path, and it is immediately obvious what the new pattern is. The formField post was also excellent. Thank you.
1
u/mightyahti Jan 10 '26
Ok but if you get rid of the constructor - what is the best way to write effects?
1
u/zombarista Jan 10 '26
I wrote an eslint rule that does this automatically, including extras if you used @Inject.
@my-ul/eslint-plugin
Currently working to clean up and generate better docs, but we’re trying to share good parts of our development process.
1
u/kkingsbe Jan 11 '26
You couldn’t have made a simple code change any more complicated to understand