Skip to content

fix(core): refine Injector.get declaration to infer correct return type - #24151

Closed
ts2do wants to merge 6 commits into
angular:masterfrom
ts2do:injector-get-fix
Closed

ts2do wants to merge 6 commits into
angular:masterfrom
ts2do:injector-get-fix

Conversation

@ts2do

@ts2do ts2do commented May 27, 2018 •

Copy link
Copy Markdown

fix(core): refine Injector.get declaration to infer correct return type

These changes address an overload resolution issue whereby Injector.get
calls would resolve to the deprecated overload, disabling all type
checks with an 'any' return type.

BREAKING CHANGE:
Applications may need to be corrected for revealed type errors.
Applications explicitly specifying the generic argument to the method
should remove the generic argument in favor of type inference.

PR Checklist

Please check if your PR fulfills the following requirements:

PR Type

What kind of change does this PR introduce?

[x] Bugfix
[ ] Feature
[ ] Code style update (formatting, local variables)
[ ] Refactoring (no functional changes, no api changes)
[ ] Build related changes
[ ] CI related changes
[ ] Documentation content changes
[ ] angular.io application / infrastructure changes
[ ] Other... Please describe:

What is the current behavior?

Given x of type Type<T>|InjectionToken<T> and y of type U (excluding THROW_IF_NOT_NULL and undefined):

  • injector.get(x, undefined) is any
  • injector.get(x, THROW_IF_NOT_NULL) is any
  • injector.get(x, y) is any
  • injector.get(x, someCondition ? y : THROW_IF_NOT_NULL) is any

What is the new behavior?

  • injector.get(x, undefined) is T
  • injector.get(x, THROW_IF_NOT_NULL) is T
  • injector.get(x, y) is T|U.
  • injector.get(x, someCondition ? y : THROW_IF_NOT_NULL) is T|U

Does this PR introduce a breaking change?

[x] Yes
[ ] No

Any application injecting Injector may be affected. Applications may need to be corrected for revealed type errors. Applications explicitly specifying the generic argument to the method should remove the generic argument in favor of type inference.

These changes address an overload resolution issue whereby Injector.get
calls would resolve to the deprecated overload, disabling all type
checks with an 'any' return type.

BREAKING CHANGE:
Applications may need to be corrected for revealed type errors.
Applications explicitly specifying the generic argument to the method
should remove the generic argument in favor of type inference.
@ts2do

ts2do commented May 27, 2018 •

Copy link
Copy Markdown
Author

FYI: I'm trying to contribute from a Windows machine and am unable to build, hence the commit to manually updating the public API golden files. Any advise would be appreciated.

@vicb vicb added the area: core Issues related to the framework runtime label May 31, 2018
@ts2do

ts2do commented May 31, 2018 •

Copy link
Copy Markdown
Author

Can someone restart the automated checks? It looks like an unexpected failure for both (429 Too Many Requests).

@alxhub alxhub added this to the v7.0 milestone Jun 5, 2018
@alxhub

alxhub commented Jun 5, 2018

Copy link
Copy Markdown
Member

Hi @ts2do, and thanks for the PR. As far as I can tell, this does actually cause test failures (probably related to the breaking change).

As it is, though, I don't think we can accept this PR until Angular 7, when we can make this kind of breaking change. I definitely think we should make Injector.get() more type-safe, though, so I'm adding this to our v7 milestone.

@trotyl

trotyl commented Jul 21, 2018

Copy link
Copy Markdown
Contributor

If this will get merged in v7, possibly #11813 should be considered as well?

@angular-automatic-lock-bot

Copy link
Copy Markdown

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.

@angular-automatic-lock-bot angular-automatic-lock-bot Bot locked and limited conversation to collaborators Sep 15, 2019
Sign up for free to subscribe to this conversation on GitHub. Already have an account? Sign in.

Labels

area: core Issues related to the framework runtime cla: yes state: blocked

Projects

None yet

Development

Successfully merging this pull request may close these issues.

6 participants