angular: Invalid `inject()` function typing
Which @angular/* package(s) are the source of the bug?
core
Is this a regression?
No
Description
inject()
's type description seems to be invalid:
https://github.com/angular/angular/blob/5cfde8b23e72cff4c9359f765a462b474bb32631/packages/core/src/di/injector_compatibility.ts#L78-L80
Return type should be T | null
only if flag is set to InjectFlags.Optional
meaning that the signature should be something like this:
export function ɵɵinject<T>(token: ProviderToken<T>): T;
export function ɵɵinject<T>(token: ProviderToken<T>, flags?: InjectFlags.Default | InjectFlags...): T;
export function ɵɵinject<T>(token: ProviderToken<T>, flags?: InjectFlags.Optional): T | null;
export function ɵɵinject<T>(token: ProviderToken<T>, flags = InjectFlags.Default): T|null
Please provide a link to a minimal reproduction of the bug
No response
Please provide the exception or error you saw
No response
Please provide the environment you discovered this bug in (run ng version
)
No response
Anything else?
No response
About this issue
- Original URL
- State: closed
- Created 2 years ago
- Reactions: 7
- Comments: 17 (16 by maintainers)
Commits related to this issue
- feat(core): options object to supercede bit flags for `inject()` `inject()` originated as a private API and was made public to support `InjectionToken` factories in Ivy. For code-size and performance... — committed to alxhub/angular by alxhub 2 years ago
- feat(core): options object to supersede bit flags for `inject()` `inject()` originated as a private API and was made public to support `InjectionToken` factories in Ivy. For code-size and performance... — committed to alxhub/angular by alxhub 2 years ago
- feat(core): options object to supersede bit flags for `inject()` `inject()` originated as a private API and was made public to support `InjectionToken` factories in Ivy. For code-size and performance... — committed to alxhub/angular by alxhub 2 years ago
- feat(core): options object to supersede bit flags for `inject()` `inject()` originated as a private API and was made public to support `InjectionToken` factories in Ivy. For code-size and performance... — committed to alxhub/angular by alxhub 2 years ago
- feat(core): options object to supersede bit flags for `inject()` `inject()` originated as a private API and was made public to support `InjectionToken` factories in Ivy. For code-size and performance... — committed to alxhub/angular by alxhub 2 years ago
- feat(core): options object to supersede bit flags for `inject()` `inject()` originated as a private API and was made public to support `InjectionToken` factories in Ivy. For code-size and performance... — committed to alxhub/angular by alxhub 2 years ago
@dario-piotrowicz good question. I think it’d be valuable to keep the mapping between the current flags (which also map to the decorators like
@Self
,@Host
, etc) and the inject flags, so it’s easier for developers to transition to the options object. However, we should also consider this improvement (may be we can add extra flags, which would be just aliases) and we can have an additional discussion to decide on the final set of flags once the draft PR is ready.Quick update: we’ve discussed this topic with the team and we want to move forward with a PR that would:
inject
andInjector.get
functions (theInjector.get
should be a separate commit), while keeping the support for the current format as well for backwards compatibilityOne more thing thing to consider is the difference between the
inject
andInjector.get
signatures: theInjector.get
has an extra argument to provide a default value if none was found in the DI. Ideally, it’d be great to fully aligninject
andInjector.get
, but it would be a separate effort.@yjaaidi please let us know if you might be interested in creating a PR to add support for the options object.
A PR is welcome, that would allow for a TGP (running all g3 tests) to see what the impact of such a change would be.