-
Notifications
You must be signed in to change notification settings - Fork 24.8k
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Deprecate @Host()
, @Inject()
, @Optional()
, @Self()
& @SkipSelf()
#50439
Comments
@Host()
, @Inject()
, @Optional()
, @Self()
& @SkipSelf()
So, instead of this,
the new code would look like that?
It's OK then, except when someone who makes the
then it's not really OK to me. Though that is already possible to do at this point, but deprecating the decorators will likely force many to use the shorter option despite its drawbacks regarding extension and reuse. I hope I'm wrong or just don't get the idea 👀 |
But it's not certain sure that parameter decorators won't be included in the final spec. There are still chances for it. |
The only alternative would be: class A {
private _something = inject(SOMETHING);
} But, this could become possible in the future: class A {
constructor(private _something = inject(SOMETHING)) {}
} @alxhub gave it a try but there were some "misuses" of default initializers that would break in that case. Cf. #48980 (comment) About child overriding parent depsThat said, I think that child class B shouldn't override its parent constructor parameters for two reasons:
If A wants to allow its child classes to override the dependencies without using DI, then you might need something different. class A {
private _something = inject(SOMETHING);
protected setSomething(something: Something) {
// @todo throw an error if you don't want to allow this to be changed after A is instantiated.
this._something = something;
}
} Of course, I would recommend relying on DI instead. Dependency Injection vs Service LocatorSome might argue that using HarmonizationWhile this is not the main argument, Usage with different buildersUsing You can give it a try here: https://github.com/yjaaidi/experiments/tree/angular-16 pnpm install
pnpm start --open # vite + angular esbuild
pnpm vite --open # vite + JIT and compare the DX by yourself. What do you think? |
@mlc-mlapis true! But let's consider both possibilities: B. if parameter decorators don't happen and TypeScript 6 totally drops experimental decorators for example, then we are stuck and we'd quickly need to migrate. While some nice schematic can fix that, there could be some edge cases that won't work. Finally, as mentioned in my previous comment, |
No please do not do this! |
Hey @Jordan-Hall! |
I think the standards are not yet finished, they a new proposal to expand on the current stage 3 decorators https://github.com/tc39/proposal-class-method-parameter-decorators. I strongly believe that angular should use compile logic themselves to handle dependency injection i(if required) until we know what happens with decorators proposals. One of the main reason I love angular is the use of the DI. Moving it to a function call would be horrible. The only way I would support this move, is if angular opens up the DI system for 3rd party replacement. Also for your comment about surviving vite and other compilers. Most compiler for angular uses Ntsc under the hood or delegate alot to them. I paused my custom compiler with swc to work on a custom lexer parser for angular first (makes life easier). But adding the support for vite and swc for the compiler was super easy |
Well, it's essentially what I was talking about but with the default initializers (forgot about them since I try not to use it mostly 😅), this would do! 👍
Can't agree with that, generally. First of all, Second, if Also don't really see a big problem with parameters order coupling
Surely I was talking with the DI in mind. The point is, if you Also there are use cases when parent must be stopped from injecting something. For example, you create PS. I think it basically comes down to having a choice on how to do things, but lack of it makes things much harder than it should be in time of need. |
As an interested bystander and someone who heavily uses DI, I'd like to make a few points.
This is a really nasty anti-pattern, and the effects of it are exarcerbated by it being private: how would you override that in testing? I'm guessing you'd do Your code above is actually using something called the Service Locator pattern, which is The main advantage of constructor parameter decoration is that all the dependencies When you move to the Service Locator pattern,
This is a good compromise, as the consumer can now see what injectables the service requires.
The main problem with the code above is that, should the constructor (or any methods it calls)
What matters is that consumers of a service understands its dependencies without having
I don't think I'd be reaching to say most people use
I'm not familiar with Angular's codebase, though I do note that their implementation of
I think holding off is the best solution right now. The solutions proposed seem half-baked, |
Running into this challenge, as I rewrote my decorators to comply with the new decorator api. Is there a workaround for Simply removing it gives me errors where I have been using it before.
I also tried:
=> Same error. |
@Eraldo Arent't you looking for |
If I do this in my constructor: ...
constructor(
// Used to work before I disabled the experimentalDecorators:
// @Optional() private ionRouterOutlet: IonRouterOutlet,
private ionRouterOutlet: IonRouterOutlet = inject(IonRouterOutlet, { optional: true, }) // <- throws an error
) {} I still get the same |
You can’t use inject as default value in the constructor. See #47606 The inject has to happen inside the body of the constructor. |
This is a larger question of moving away from param decorators that would detailed design, RFC etc. - we track it in our backlog already and I don't think it is a good fit for an issue tracker. |
This issue has been automatically locked due to inactivity. Read more about our automatic conversation locking policy. This action has been performed automatically by a bot. |
Which @angular/* package(s) are relevant/related to the feature request?
core
Description
Considering TypeScript 5's support for ES Decorators which do not currently support parameter decorators, we could deprecate all parameter decorators and recommend the usage of
inject()
instead.This would allow us to safely disable experimental decorators in the future.
Proposed solution
Deprecate parameter decorators.
Additional Details & Motivations
#50439 (comment)
The text was updated successfully, but these errors were encountered: