Join GitHub today
GitHub is home to over 50 million developers working together to host and review code, manage projects, and build software together.
Sign updocs: Specify behaviour of providers for generic types #35497
Comments
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment


Description
Right now, the documentation doesn't specify what happens when services are injected using type tokens with generic parameters (such as
MyGenericService<number>orMyGenericService<string>), or at least I haven't found such a place.The behaviour can be tested - currently different variations of generic service have the same underlying instance (unless overriden in injection hierarchy). However, without it being documented I can't be certain it's an intended/accepted behaviour, and thus can't safely rely on it.
An example use-case where I'd like to rely on this behaviour:
MyGenericService<TData>, where TData would be used for stronger type-checking of type-specific properties/methodsMyGenericService<ConcreteData>in a component/directive/service that passes/receivesConcreteDatainstances to/from the serviceMyGenericService<any>to other component/directive/service that doesn't rely on type-specific properties/methods of the serviceWhat's the affected URL?**
https://angular.io/guide/dependency-injection-providers#the-provider-object-literal
Reproduction Steps**
Given a setup like this and an instance of TestComponent somewhere:
I get the following console output:
From my tests I've found that:
It's a reasonable behaviour, especially since there's no way to define an injection token with generic type argument in the first place. However, tests are needed to confirm this behaviour. Also, there's no guarantee this behaviour won't change, especially if sometime in the future Angular would be able to obtain generic type information (which now is unavailable, according to this SO answer).
Expected vs Actual Behavior**
I'd like to have the behaviour of generic type providers specified in the documentation. "The Provider object literal" section of Dependency Injection >> DI Providers page seems like a good place to specify that.
Additional tests might be needed to ensure the current behaviour doesn't break, if none are present at the moment.
Browser info
Google Chrome 79