UNPKG

6.12 kBTypeScriptView Raw
1/**
2 * @license
3 * Copyright Google Inc. All Rights Reserved.
4 *
5 * Use of this source code is governed by an MIT-style license that can be
6 * found in the LICENSE file at https://angular.io/license
7 */
8import { Type } from './facade/type';
9/**
10 * @whatItDoes Configures the {@link Injector} to return an instance of `Type` when `Type' is used
11 * as token.
12 * @howToUse
13 * ```
14 * @Injectable()
15 * class MyService {}
16 *
17 * const provider: TypeProvider = MyService;
18 * ```
19 *
20 * @description
21 *
22 * Create an instance by invoking the `new` operator and supplying additional arguments.
23 * This form is a short form of `TypeProvider`;
24 *
25 * For more details, see the {@linkDocs guide/dependency-injection "Dependency Injection Guide"}.
26 *
27 * ### Example
28 *
29 * {@example core/di/ts/provider_spec.ts region='TypeProvider'}
30 *
31 * @stable
32 */
33export interface TypeProvider extends Type<any> {
34}
35/**
36 * @whatItDoes Configures the {@link Injector} to return a value for a token.
37 * @howToUse
38 * ```
39 * const provider: ValueProvider = {provide: 'someToken', useValue: 'someValue'};
40 * ```
41 *
42 * @description
43 * For more details, see the {@linkDocs guide/dependency-injection "Dependency Injection Guide"}.
44 *
45 * ### Example
46 *
47 * {@example core/di/ts/provider_spec.ts region='ValueProvider'}
48 *
49 * @stable
50 */
51export interface ValueProvider {
52 /**
53 * An injection token. (Typically an instance of `Type` or `InjectionToken`, but can be `any`).
54 */
55 provide: any;
56 /**
57 * The value to inject.
58 */
59 useValue: any;
60 /**
61 * If true, then injector returns an array of instances. This is useful to allow multiple
62 * providers spread across many files to provide configuration information to a common token.
63 *
64 * ### Example
65 *
66 * {@example core/di/ts/provider_spec.ts region='MultiProviderAspect'}
67 */
68 multi?: boolean;
69}
70/**
71 * @whatItDoes Configures the {@link Injector} to return an instance of `useClass` for a token.
72 * @howToUse
73 * ```
74 * @Injectable()
75 * class MyService {}
76 *
77 * const provider: ClassProvider = {provide: 'someToken', useClass: MyService};
78 * ```
79 *
80 * @description
81 * For more details, see the {@linkDocs guide/dependency-injection "Dependency Injection Guide"}.
82 *
83 * ### Example
84 *
85 * {@example core/di/ts/provider_spec.ts region='ClassProvider'}
86 *
87 * Note that following two providers are not equal:
88 * {@example core/di/ts/provider_spec.ts region='ClassProviderDifference'}
89 *
90 * @stable
91 */
92export interface ClassProvider {
93 /**
94 * An injection token. (Typically an instance of `Type` or `InjectionToken`, but can be `any`).
95 */
96 provide: any;
97 /**
98 * Class to instantiate for the `token`.
99 */
100 useClass: Type<any>;
101 /**
102 * If true, then injector returns an array of instances. This is useful to allow multiple
103 * providers spread across many files to provide configuration information to a common token.
104 *
105 * ### Example
106 *
107 * {@example core/di/ts/provider_spec.ts region='MultiProviderAspect'}
108 */
109 multi?: boolean;
110}
111/**
112 * @whatItDoes Configures the {@link Injector} to return a value of another `useExisting` token.
113 * @howToUse
114 * ```
115 * const provider: ExistingProvider = {provide: 'someToken', useExisting: 'someOtherToken'};
116 * ```
117 *
118 * @description
119 * For more details, see the {@linkDocs guide/dependency-injection "Dependency Injection Guide"}.
120 *
121 * ### Example
122 *
123 * {@example core/di/ts/provider_spec.ts region='ExistingProvider'}
124 *
125 * @stable
126 */
127export interface ExistingProvider {
128 /**
129 * An injection token. (Typically an instance of `Type` or `InjectionToken`, but can be `any`).
130 */
131 provide: any;
132 /**
133 * Existing `token` to return. (equivalent to `injector.get(useExisting)`)
134 */
135 useExisting: any;
136 /**
137 * If true, then injector returns an array of instances. This is useful to allow multiple
138 * providers spread across many files to provide configuration information to a common token.
139 *
140 * ### Example
141 *
142 * {@example core/di/ts/provider_spec.ts region='MultiProviderAspect'}
143 */
144 multi?: boolean;
145}
146/**
147 * @whatItDoes Configures the {@link Injector} to return a value by invoking a `useFactory`
148 * function.
149 * @howToUse
150 * ```
151 * function serviceFactory() { ... }
152 *
153 * const provider: FactoryProvider = {provide: 'someToken', useFactory: serviceFactory, deps: []};
154 * ```
155 *
156 * @description
157 * For more details, see the {@linkDocs guide/dependency-injection "Dependency Injection Guide"}.
158 *
159 * ### Example
160 *
161 * {@example core/di/ts/provider_spec.ts region='FactoryProvider'}
162 *
163 * Dependencies can also be marked as optional:
164 * {@example core/di/ts/provider_spec.ts region='FactoryProviderOptionalDeps'}
165 *
166 * @stable
167 */
168export interface FactoryProvider {
169 /**
170 * An injection token. (Typically an instance of `Type` or `InjectionToken`, but can be `any`).
171 */
172 provide: any;
173 /**
174 * A function to invoke to create a value for this `token`. The function is invoked with
175 * resolved values of `token`s in the `deps` field.
176 */
177 useFactory: Function;
178 /**
179 * A list of `token`s which need to be resolved by the injector. The list of values is then
180 * used as arguments to the `useFactory` function.
181 */
182 deps?: any[];
183 /**
184 * If true, then injector returns an array of instances. This is useful to allow multiple
185 * providers spread across many files to provide configuration information to a common token.
186 *
187 * ### Example
188 *
189 * {@example core/di/ts/provider_spec.ts region='MultiProviderAspect'}
190 */
191 multi?: boolean;
192}
193/**
194 * @whatItDoes Describes how the {@link Injector} should be configured.
195 * @howToUse
196 * See {@link TypeProvider}, {@link ValueProvider}, {@link ClassProvider}, {@link ExistingProvider},
197 * {@link FactoryProvider}.
198 *
199 * @description
200 * For more details, see the {@linkDocs guide/dependency-injection "Dependency Injection Guide"}.
201 *
202 * @stable
203 */
204export declare type Provider = TypeProvider | ValueProvider | ClassProvider | ExistingProvider | FactoryProvider | any[];