question-mark
Stuck on an issue?

Lightrun Answers was designed to reduce the constant googling that comes with debugging 3rd party libraries. It collects links to all the places you might be looking at while hunting down a tough bug.

And, if you’re still stuck at the end, we’re happy to hop on a call to see how we can help out.

Invalid `inject()` function typing

See original GitHub issue

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

Issue Analytics

  • State:closed
  • Created a year ago
  • Reactions:7
  • Comments:17 (16 by maintainers)

github_iconTop GitHub Comments

1reaction
AndrewKushnircommented, Jun 9, 2022

Another thing, it may be a bit of a silly one, but if we are going to replace the flags with an object could we maybe rename self to something like onlySelf, untilSelf or similar? I think self is a bit less clear, or is that just me?

@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.

1reaction
AndrewKushnircommented, Jun 9, 2022

Quick update: we’ve discussed this topic with the team and we want to move forward with a PR that would:

  • add the support for an object as the second argument in the inject and Injector.get functions (the Injector.get should be a separate commit), while keeping the support for the current format as well for backwards compatibility
  • add the deprecation annotation to the type signatures where an old format is used, with a recommendation to use a new object-based format

One more thing thing to consider is the difference between the inject and Injector.get signatures: the Injector.get has an extra argument to provide a default value if none was found in the DI. Ideally, it’d be great to fully align inject and Injector.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.

Read more comments on GitHub >

github_iconTop 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 >

github_iconTop Related Medium Post

No results found

github_iconTop Related StackOverflow Question

No results found

github_iconTroubleshoot Live Code

Lightrun enables developers to add logs, metrics and snapshots to live code - no restarts or redeploys required.
Start Free

github_iconTop Related Reddit Thread

No results found

github_iconTop Related Hackernoon Post

No results found

github_iconTop Related Tweet

No results found

github_iconTop Related Dev.to Post

No results found

github_iconTop Related Hashnode Post

No results found