Skip to content

Commit

Permalink
feat(common): support empty @Inject() on constructor-based injection
Browse files Browse the repository at this point in the history
  • Loading branch information
micalevisk committed Apr 13, 2024
1 parent 8b4dbb3 commit 684ce3e
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 684ce3e

Please sign in to comment.