Skip to content

Commit

Permalink
feat(core): support Provider type in Injector.create (#49587)
Browse files Browse the repository at this point in the history
This commit updates the Injector.create function to accept the `Provider` type in addition to the `StaticProvider` type. This should make it easier to work with the Injector.create function and have less type casts if you have a list of `Provider`s available.

PR Close #49587
  • Loading branch information
AndrewKushnir authored and alxhub committed Jul 28, 2023
1 parent 5061311 commit cdaa2a8
Show file tree
Hide file tree
Showing 3 changed files with 10 additions and 7 deletions.
2 changes: 1 addition & 1 deletion goldens/public-api/core/index.md
Expand Up @@ -795,7 +795,7 @@ export abstract class Injector {
// @deprecated (undocumented)
static create(providers: StaticProvider[], parent?: Injector): Injector;
static create(options: {
providers: StaticProvider[];
providers: Array<Provider | StaticProvider>;
parent?: Injector;
name?: string;
}): Injector;
Expand Down
6 changes: 3 additions & 3 deletions packages/core/src/di/create_injector.ts
Expand Up @@ -10,7 +10,7 @@ import {EMPTY_ARRAY} from '../util/empty';
import {stringify} from '../util/stringify';

import {Injector} from './injector';
import {StaticProvider} from './interface/provider';
import {Provider, StaticProvider} from './interface/provider';
import {importProvidersFrom} from './provider_collection';
import {getNullInjector, R3Injector} from './r3_injector';
import {InjectorScope} from './scope';
Expand All @@ -22,7 +22,7 @@ import {InjectorScope} from './scope';
*/
export function createInjector(
defType: /* InjectorType<any> */ any, parent: Injector|null = null,
additionalProviders: StaticProvider[]|null = null, name?: string): Injector {
additionalProviders: Array<Provider|StaticProvider>|null = null, name?: string): Injector {
const injector =
createInjectorWithoutInjectorInstances(defType, parent, additionalProviders, name);
injector.resolveInjectorInitializers();
Expand All @@ -36,7 +36,7 @@ export function createInjector(
*/
export function createInjectorWithoutInjectorInstances(
defType: /* InjectorType<any> */ any, parent: Injector|null = null,
additionalProviders: StaticProvider[]|null = null, name?: string,
additionalProviders: Array<Provider|StaticProvider>|null = null, name?: string,
scopes = new Set<InjectorScope>()): R3Injector {
const providers = [
additionalProviders || EMPTY_ARRAY,
Expand Down
9 changes: 6 additions & 3 deletions packages/core/src/di/injector.ts
Expand Up @@ -13,7 +13,7 @@ import {InjectorMarkers} from './injector_marker';
import {INJECTOR} from './injector_token';
import {ɵɵdefineInjectable} from './interface/defs';
import {InjectFlags, InjectOptions} from './interface/injector';
import {StaticProvider} from './interface/provider';
import {Provider, StaticProvider} from './interface/provider';
import {NullInjector} from './null_injector';
import {ProviderToken} from './provider_token';

Expand Down Expand Up @@ -104,11 +104,14 @@ export abstract class Injector {
* @returns The new injector instance.
*
*/
static create(options: {providers: StaticProvider[], parent?: Injector, name?: string}): Injector;
static create(options:
{providers: Array<Provider|StaticProvider>, parent?: Injector, name?: string}):
Injector;


static create(
options: StaticProvider[]|{providers: StaticProvider[], parent?: Injector, name?: string},
options: StaticProvider[]|
{providers: Array<Provider|StaticProvider>, parent?: Injector, name?: string},
parent?: Injector): Injector {
if (Array.isArray(options)) {
return createInjector({name: ''}, parent, options, '');
Expand Down

0 comments on commit cdaa2a8

Please sign in to comment.