Repository navigation
Closed
Labels
area: coreIssues related to the framework runtimeIssues related to the framework runtimearea: testingIssues related to Angular testing features, such as TestBedIssues related to Angular testing features, such as TestBedfeatureLabel used to distinguish feature request from other issuesLabel used to distinguish feature request from other issues
Milestone
Description
I'm submitting a...
[ ] Regression (a behavior that used to work and stopped working in a new release)
[ ] Bug report
[ ] Performance issue
[x] Feature request
[ ] Documentation issue or request
[ ] Support request => Please do not submit support request here, instead see https://github.com/angular/angular/blob/master/CONTRIBUTING.md#question
[ ] Other... Please describe:
Current behavior
The get static method of TestBed class does not return a typed object.
const service = TestBed.get(MyService); // service is typed as anyExpected behavior
The get static method should return a typed object.
const service = TestBed.get(MyService); // service should be typed as MyServiceMinimal reproduction of the problem with instructions
Look examples.
What is the motivation / use case for changing the behavior?
This would improve type safety.
Environment
Angular version: 6.1.9
Others:
A possible implementation using new helper types from TypeScript would be:
static get<T extends new (...args: any[]) => any>(token: T, notFoundValue?: T | InstanceType<T>): InstanceType<T>;I'm not sure if the notFoundValue should be a constructor or an instance, so I typed it as both, but fell free to update it as is actually needed. I tested that signature and it actually returns a typed object as wanted.
Metadata
Metadata
Assignees
Labels
area: coreIssues related to the framework runtimeIssues related to the framework runtimearea: testingIssues related to Angular testing features, such as TestBedIssues related to Angular testing features, such as TestBedfeatureLabel used to distinguish feature request from other issuesLabel used to distinguish feature request from other issues
Activity
trotyl commentedon Oct 17, 2018
Relates to #18695, #23611, #24151
harshabada commentedon Oct 17, 2018
did u added the above service in providers
mhevery commentedon Oct 25, 2018
A PR would be great!
michaeljota commentedon Nov 13, 2018
@mhegazy I'll try to get into it. No promises.
Goodwine commentedon Jan 22, 2019
@michaeljota hiya, I was facing this same issue today and found your report, are you working on a PR? otherwise I'm thinking I can try to send one, just didn't want to step over
michaeljota commentedon Jan 22, 2019
Hi @Goodwine. I haven't get a chance to look at this. You can give it a look if you want. Thanks!
Goodwine commentedon Jan 23, 2019
My proposal is to use these types -
The reason for using
unknownwhen the value isn't either anInjectionTokenor aTypeis because the desired type is actually an abstract class, and this will force the user to cast to the correct value.There is one problem with this approach though. What if the user wants to use the abstract class interface? Or what if the user knows the real value? Unfortunately this is kinda annoying to do in TS because if we have an abstract class
Foo, the return value would betypeof Foo. We're only able to get away with it with concrete classes because angular has this fancy typeTypewhich extends anything with a constructor so for a concrete classBar, the inferred value isType<Bar>which allows for easily returning of typeBarrather thantypeof BarPerhaps the return value should be
neverso that the user isn't even allowed to cast the return value and instead they would have to cast the argument. For example:Problem is that this could be anything and the user could cast to whatever they want, so maybe returning
unknownis best?michaeljota commentedon Jan 23, 2019
I would consider an approach that reduce type casting, as I think is just about that.
michaeljota commentedon Jan 23, 2019
@Goodwine What do you think about something like this:
I think this would reduce type casting. I'm want to be sure that I understand the usage of
Type<T>in your examples, but I think this can be done with conditional typing.10 remaining items
michaeljota commentedon Jan 24, 2019
Why would you want to get null if not found? I'm trying to understand that scenario.
Goodwine commentedon Jan 24, 2019
That's just to support the existing use of
get(NgZone, null), I'd rather have one type as mentioned above.To be honest I'd like to get rid of the second param for
return this when the other was not found, a variadic parameter for option (the way it's currently used) would be OK with me tho.Goodwine commentedon Mar 13, 2019
Talking with the angular team (I'm not part of the angular team), I'll actually send PRs in the following order:
@deprecateto current function signature and add same signature asInjector.get, however it would still returnany.feat(core): Add "AbstractType<T>" interface (#29295)
fix(core): Deprecate TestBed.get(...):any (#29290)
feat(core): Add "AbstractType<T>" interface (angular#29295)
fix(core): Deprecate TestBed.get(...):any (angular#29290)
Goodwine commentedon Aug 19, 2019
Per discussion with @vikerman, the Angular team can't accept such a breaking change at this point, so the approach we'll now take is to create a new method
TestBed.injectwhich will be type-safe. And deprecate TestBed.get.feat(core): Introduce TestBed.inject to replace TestBed.get (#32200)
feat(core): Introduce TestBed.inject to replace TestBed.get (angular#…
angular-automatic-lock-bot commentedon Oct 13, 2019
This issue has been automatically locked due to inactivity.
Please file a new issue if you are encountering a similar or related problem.
Read more about our automatic conversation locking policy.
This action has been performed automatically by a bot.