Skip to content

Commit

Permalink
Merge pull request #13428 from micalevisk/feat/issue-13426
Browse files Browse the repository at this point in the history
feat(common): support empty `@Inject()` on constructor-based injection
  • Loading branch information
kamilmysliwiec authored Jun 3, 2024
2 parents aca15c9 + 684ce3e commit 4b07cb2
Show file tree
Hide file tree
Showing 2 changed files with 9 additions and 2 deletions.
9 changes: 8 additions & 1 deletion packages/common/decorators/core/inject.decorator.ts
Original file line number Diff line number Diff line change
@@ -1,4 +1,5 @@
import {
PARAMTYPES_METADATA,
PROPERTY_DEPS_METADATA,
SELF_DECLARED_DEPS_METADATA,
} from '../../constants';
Expand Down Expand Up @@ -36,8 +37,14 @@ import { isUndefined } from '../../utils/shared.utils';
export function Inject<T = any>(
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 =
Expand Down
2 changes: 1 addition & 1 deletion packages/core/test/injector/injector.spec.ts
Original file line number Diff line number Diff line change
Expand Up @@ -34,7 +34,7 @@ describe('Injector', () => {

constructor(
public one: DependencyOne,
public two: DependencyTwo,
@Inject() public two: DependencyTwo,
) {}
}

Expand Down

0 comments on commit 4b07cb2

Please sign in to comment.