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): introduce EnvironmentProviders wrapper type #47669
Conversation
99c2702
to
e953d3d
Compare
e953d3d
to
5722992
Compare
fa5888f
to
1f63122
Compare
This commit introduces the `EnvironmentProviders` interface, but does not yet export it as public API.
This commit modifies `R3Injector` and other code in Angular that deals with providers, to handle `EnvironmentProviders` objects as well as normal `Provider` types. There is no user-visible impact to this change, but it prepares the core of the DI system for the introduction of `EnvironmentProviders` as a public feature.
This commit introduces a new type `EnvironmentProviders` which can be used in contexts where Angular accepted `Provider`s destined for `EnvironmentInjector`s. This includes contexts such as `@NgModule.providers` and `Route.providers`. The new type is useful for preventing such providers from accidentally ending up in `@Component.providers`. It can be used as the return type of provider functions (such as `provideRouter`) to enforce this safety. Because `Provider` allows `any[]` nested arrays, the compile-time safety provided by `EnvironmentProviders` is easily circumvented. However, the runtime shape of `EnvironmentProviders` is not compatible with component injectors and will result in a runtime error if it leaks through (NG0207). A new function `makeEnvironmentProviders` is used to construct this new type from an array of providers. The existing `importProvidersFrom` operation previously returned a very similar type `ImportedNgModuleProviders` which had the same goal. This machinery is switched over to use the new `EnvironmentProviders` interface instead (in fact, `ImportedNgModuleProviders` is now just an alias to `EnvironmentProviders`).
This commit switches `provideRouter()` to return the new `EnvironmentProviders` wrapper type, preventing it from being accidentally (or intentionally) included in `@Component.providers`.
1f63122
to
640e596
Compare
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.
Looks great 👍
ɵbrand: 'EnvironmentProviders'; | ||
}; | ||
|
||
export interface InternalEnvironmentProviders extends EnvironmentProviders { |
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.
nit: may be add a quick comment on why we need this type (I think it's covered in the ɵfromNgModule
field description, may be just add a version of it as a comment about this function)?
* Wrap an array of `Provider`s into `EnvironmentProviders`, preventing them from being accidentally | ||
* referenced in `@Component in a component injector. |
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.
* Wrap an array of `Provider`s into `EnvironmentProviders`, preventing them from being accidentally | |
* referenced in `@Component in a component injector. | |
* Wrap an array of `Provider`s into `EnvironmentProviders`, preventing them from being accidentally | |
* referenced in `@Component` or `@Directive` in a component injector. |
deepForEach(defProviders, provider => { | ||
ngDevMode && validateProvider(provider, defProviders as SingleProvider[], injectorType); | ||
providersOut.push(provider); | ||
deepForEachProvider(defProviders, provider => { |
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.
Just curious if we can type it here (vs adding as
)?
deepForEachProvider(defProviders, provider => { | |
deepForEachProvider(defProviders, (provider: SingleProvider) => { |
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.
Reviewed-for: public-api
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.
reviewed-for: public-api
This PR was merged into the repository by commit 07017a7. |
This commit modifies `R3Injector` and other code in Angular that deals with providers, to handle `EnvironmentProviders` objects as well as normal `Provider` types. There is no user-visible impact to this change, but it prepares the core of the DI system for the introduction of `EnvironmentProviders` as a public feature. PR Close #47669
This commit introduces a new type `EnvironmentProviders` which can be used in contexts where Angular accepted `Provider`s destined for `EnvironmentInjector`s. This includes contexts such as `@NgModule.providers` and `Route.providers`. The new type is useful for preventing such providers from accidentally ending up in `@Component.providers`. It can be used as the return type of provider functions (such as `provideRouter`) to enforce this safety. Because `Provider` allows `any[]` nested arrays, the compile-time safety provided by `EnvironmentProviders` is easily circumvented. However, the runtime shape of `EnvironmentProviders` is not compatible with component injectors and will result in a runtime error if it leaks through (NG0207). A new function `makeEnvironmentProviders` is used to construct this new type from an array of providers. The existing `importProvidersFrom` operation previously returned a very similar type `ImportedNgModuleProviders` which had the same goal. This machinery is switched over to use the new `EnvironmentProviders` interface instead (in fact, `ImportedNgModuleProviders` is now just an alias to `EnvironmentProviders`). PR Close #47669
) This commit switches `provideRouter()` to return the new `EnvironmentProviders` wrapper type, preventing it from being accidentally (or intentionally) included in `@Component.providers`. PR Close #47669
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. |
feat(core): introduce EnvironmentProviders wrapper type
This commit introduces a new type
EnvironmentProviders
which can be usedin contexts where Angular accepted
Provider
s destined forEnvironmentInjector
s. This includes contexts such as@NgModule.providers
and
Route.providers
.The new type is useful for preventing such providers from accidentally
ending up in
@Component.providers
. It can be used as the return type ofprovider functions (such as
provideRouter
) to enforce this safety.Because
Provider
allowsany[]
nested arrays, the compile-time safetyprovided by
EnvironmentProviders
is easily circumvented. However, theruntime shape of
EnvironmentProviders
is not compatible with componentinjectors and will result in a runtime error if it leaks through (NG0207).
A new function
makeEnvironmentProviders
is used to construct this new typefrom an array of providers.
The existing
importProvidersFrom
operation previously returned a verysimilar type
ImportedNgModuleProviders
which had the same goal. Thismachinery is switched over to use the new
EnvironmentProviders
interfaceinstead (in fact,
ImportedNgModuleProviders
is now just an alias toEnvironmentProviders
).