-
Notifications
You must be signed in to change notification settings - Fork 26.5k
feat(core): more precise type for APP_INITIALIZER token #40986
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
feat(core): more precise type for APP_INITIALIZER token #40986
Conversation
365122d
to
b0062df
Compare
ac87478
to
40ab7b5
Compare
@JoostK do you have any idea of why tests are failing with the following error:
|
Yeah, maybe this is why @AndrewKushnir marked this as breaking? The issue here is that TypeScript uses its inferred type from the returned array literal as the return type of the function declaration that is emitted into the generated .d.ts file. Since it includes angular/packages/router/src/router_module.ts Lines 587 to 598 in 4a7a649
The workaround is quite simple: add an explicit return type It's an interesting case considering the fact that this was not an issue with the named type; I'd be interested to hear what others think here w.r.t. having a named type as you initially had. |
40ab7b5
to
59bd7f3
Compare
I was under impression that this would be breaking and was going to perform some research on which scenarios can be affected, but tests identified them faster :)
I like the current implementation (without exposing additional type), I think it's a more "lightweigth" API. I've started a set of tests in Google's codebase (internal-only link) to see how that would affect apps (+ also started a global run). We can use that data as an input to the discussion and final decision. Thank you. |
Quick update after running tests in Google's codebase (all affected targets aka global run): there are no breakages caused by this change, which suggests that this changes is not that break-y (but still this is a breaking change as we've seen it with a Router example). |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
@abarghoud I've left a couple suggestions to use ReadonlyArray
instead of an Array
, but overall the change looks good and it's not causing any issues in Google's codebase.
We'd also need to update commit message to include the "BREAKING CHANGE" note. We'll followup with a proposed draft to explain in which circumstances this might be an actual breaking change.
Thank you.
@abarghoud Here's the suggestion for the commit message:
|
59bd7f3
to
90f4bd1
Compare
@JoostK @AndrewKushnir, I took into account your remarks as well as modified the commit message |
This commit updates the type of the `APP_INITIALIZER` injection token to better document the expected types of values that Angular handles. Only Promises and Observables are awaited and other types of values are ignored, so the type of `APP_INITIALIZER` has been updated to `Promise<unknown> | Observable<unknown> | void` to reflect this behavior. BREAKING CHANGE: The type of the `APP_INITIALIZER` token has been changed to more accurately reflect the types of return values that are handled by Angular. Previously, each initializer callback was typed to return `any`, this is now `Promise<unknown> | Observable<unknown> | void`. In the unlikely event that your application uses the `Injector.get` or `TestBed.inject` API to inject the `APP_INITIALIZER` token, you may need to update the code to account for the stricter type. Additionally, TypeScript may report the TS2742 error if the `APP_INITIALIZER` token is used in an expression of which its inferred type has to be emitted into a .d.ts file. To workaround this, an explicit type annotation is needed, which would typically be `Provider` or `Provider[]`. Closes angular#40729
90f4bd1
to
bc65f53
Compare
This issue has been automatically locked due to inactivity. Read more about our automatic conversation locking policy. This action has been performed automatically by a bot. |
This commit updates the type of the
APP_INITIALIZER
injection token tobetter document the expected types of values that Angular handles. Only
Promises and Observables are awaited and other types of values are ignored,
so the type of
APP_INITIALIZER
has been updated toPromise<unknown> | Observable<unknown> | void
to reflect this behavior.BREAKING CHANGE:
The type of the
APP_INITIALIZER
token has been changed to more accuratelyreflect the types of return values that are handled by Angular. Previously,
each initializer callback was typed to return
any
, this is nowPromise<unknown> | Observable<unknown> | void
. In the unlikely event thatyour application uses the
Injector.get
orTestBed.inject
API to injectthe
APP_INITIALIZER
token, you may need to update the code to account forthe stricter type.
Additionally, TypeScript may report the TS2742 error if the
APP_INITIALIZER
token is used in an expression of which its inferred type has to be emitted
into a .d.ts file. To workaround this, an explicit type annotation is needed,
which would typically be
Provider
orProvider[]
.Closes #40729
Does this PR introduce a breaking change?