Invalid `inject()` function typing
See original GitHub issueWhich @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
Issue Analytics
- State:
- Created a year ago
- Reactions:7
- Comments:17 (16 by maintainers)
Top Results From Across the Web
reactjs - Correctly typing prop injection
Here is a universal solution to props injection: import { ElementType } from "react"; import "./styles.css"; //////// // Universal utilities ...
Read more >NG0203: `inject()` must be called from an injection context ...
You see this error when you try to use the inject() function outside of the allowed injection context. The injection context is available...
Read more >Use dependency injection in .NET Azure Functions
The dependency injection container only holds explicitly registered types. The only services available as injectable types are what are setup ...
Read more >Why Angular 14's new inject() function is so amazing?
The inject() function takes an InjectionToken as a parameter and returns the value for that InjectionToken from the currently active injector.
Read more >chrome.scripting - Chrome Developers
You can use the chrome.scripting API to inject JavaScript and CSS into ... executeScript() , you can specify a function to be executed...
Read more >Top Related Medium Post
No results found
Top Related StackOverflow Question
No results found
Troubleshoot Live Code
Lightrun enables developers to add logs, metrics and snapshots to live code - no restarts or redeploys required.
Start FreeTop Related Reddit Thread
No results found
Top Related Hackernoon Post
No results found
Top Related Tweet
No results found
Top Related Dev.to Post
No results found
Top Related Hashnode Post
No results found
Top GitHub Comments
@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.