Skip to content

Commit ec01599

Browse files
iterianithePunderWoman
authored andcommitted
refactor(core): Add Constructor type to injection types (angular#62265)
This allows us to pass in constructors into the inject function PR Close angular#62265
1 parent 402eaa1 commit ec01599

File tree

4 files changed

+17
-7
lines changed

4 files changed

+17
-7
lines changed

goldens/public-api/core/primitives/di/index.api.md

Lines changed: 8 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -4,11 +4,18 @@
44
55
```ts
66

7+
// @public (undocumented)
8+
export function defineInjectable<T>(opts: {
9+
token: unknown;
10+
providedIn?: Type<any> | 'root' | 'platform' | 'any' | 'environment' | null;
11+
factory: () => T;
12+
}): unknown;
13+
714
// @public (undocumented)
815
export function getCurrentInjector(): Injector | undefined | null;
916

1017
// @public (undocumented)
11-
export function inject<T>(token: InjectionToken<T>, options?: unknown): T | NotFound;
18+
export function inject<T>(token: InjectionToken<T> | Constructor<T>, options?: unknown): T | NotFound;
1219

1320
// @public
1421
export interface InjectionToken<T> {

packages/core/primitives/di/index.ts

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -11,4 +11,4 @@ export type {Injector} from './src/injector';
1111
export {NOT_FOUND, NotFoundError, isNotFound} from './src/not_found';
1212
export type {NotFound} from './src/not_found';
1313
export type {InjectionToken, ɵɵInjectableDeclaration} from './src/injection_token';
14-
export {registerInjectable} from './src/injection_token';
14+
export {defineInjectable, registerInjectable} from './src/injection_token';

packages/core/primitives/di/src/injection_token.ts

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -80,7 +80,7 @@ export function defineInjectable<T>(opts: {
8080
} as ɵɵInjectableDeclaration<T>;
8181
}
8282

83-
type Constructor<T> = Function & {prototype: T};
83+
export type Constructor<T> = Function & {prototype: T};
8484

8585
export function registerInjectable<T>(
8686
ctor: unknown,

packages/core/primitives/di/src/injector.ts

Lines changed: 7 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -6,7 +6,7 @@
66
* found in the LICENSE file at https://angular.dev/license
77
*/
88

9-
import {InjectionToken} from './injection_token';
9+
import {Constructor, InjectionToken} from './injection_token';
1010
import {NotFound, NOT_FOUND} from './not_found';
1111

1212
export interface Injector {
@@ -33,10 +33,13 @@ export function setCurrentInjector(
3333
return former;
3434
}
3535

36-
export function inject<T>(token: InjectionToken<T>, options?: unknown): T | NotFound {
36+
export function inject<T>(
37+
token: InjectionToken<T> | Constructor<T>,
38+
options?: unknown,
39+
): T | NotFound {
3740
const currentInjector = getCurrentInjector();
38-
if (!currentInjector) {
41+
if (!currentInjector || !(token as InjectionToken<T>).ɵprov) {
3942
return NOT_FOUND;
4043
}
41-
return currentInjector.retrieve(token, options);
44+
return currentInjector.retrieve(token as InjectionToken<T>, options);
4245
}

0 commit comments

Comments
 (0)