Skip to content

Commit 684ce3e

Browse files
committed
feat(common): support empty @Inject() on constructor-based injection
1 parent 8b4dbb3 commit 684ce3e

File tree

2 files changed

+9
-2
lines changed

2 files changed

+9
-2
lines changed

packages/common/decorators/core/inject.decorator.ts

+8-1
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,5 @@
11
import {
2+
PARAMTYPES_METADATA,
23
PROPERTY_DEPS_METADATA,
34
SELF_DECLARED_DEPS_METADATA,
45
} from '../../constants';
@@ -36,8 +37,14 @@ import { isUndefined } from '../../utils/shared.utils';
3637
export function Inject<T = any>(
3738
token?: T,
3839
): PropertyDecorator & ParameterDecorator {
40+
const injectCallHasArguments = arguments.length > 0;
41+
3942
return (target: object, key: string | symbol | undefined, index?: number) => {
40-
const type = token || Reflect.getMetadata('design:type', target, key);
43+
let type = token || Reflect.getMetadata('design:type', target, key);
44+
// Try to infer the token in a constructor-based injection
45+
if (!type && !injectCallHasArguments) {
46+
type = Reflect.getMetadata(PARAMTYPES_METADATA, target, key)?.[index];
47+
}
4148

4249
if (!isUndefined(index)) {
4350
let dependencies =

packages/core/test/injector/injector.spec.ts

+1-1
Original file line numberDiff line numberDiff line change
@@ -34,7 +34,7 @@ describe('Injector', () => {
3434

3535
constructor(
3636
public one: DependencyOne,
37-
public two: DependencyTwo,
37+
@Inject() public two: DependencyTwo,
3838
) {}
3939
}
4040

0 commit comments

Comments
 (0)