diff --git a/packages/common/decorators/core/inject.decorator.ts b/packages/common/decorators/core/inject.decorator.ts index e8f559124a6..d06593dbee1 100644 --- a/packages/common/decorators/core/inject.decorator.ts +++ b/packages/common/decorators/core/inject.decorator.ts @@ -1,4 +1,5 @@ import { + PARAMTYPES_METADATA, PROPERTY_DEPS_METADATA, SELF_DECLARED_DEPS_METADATA, } from '../../constants'; @@ -36,8 +37,14 @@ import { isUndefined } from '../../utils/shared.utils'; export function Inject( token?: T, ): PropertyDecorator & ParameterDecorator { + const injectCallHasArguments = arguments.length > 0; + return (target: object, key: string | symbol | undefined, index?: number) => { - const type = token || Reflect.getMetadata('design:type', target, key); + let type = token || Reflect.getMetadata('design:type', target, key); + // Try to infer the token in a constructor-based injection + if (!type && !injectCallHasArguments) { + type = Reflect.getMetadata(PARAMTYPES_METADATA, target, key)?.[index]; + } if (!isUndefined(index)) { let dependencies = diff --git a/packages/core/test/injector/injector.spec.ts b/packages/core/test/injector/injector.spec.ts index d830067f4df..6a221410764 100644 --- a/packages/core/test/injector/injector.spec.ts +++ b/packages/core/test/injector/injector.spec.ts @@ -34,7 +34,7 @@ describe('Injector', () => { constructor( public one: DependencyOne, - public two: DependencyTwo, + @Inject() public two: DependencyTwo, ) {} }