typescript-ioc icon indicating copy to clipboard operation
typescript-ioc copied to clipboard

Invalid @Inject Decorator declaration on property

Open jhd-lego opened this issue 4 years ago • 5 comments

I seem to be hitting an error when I am trying to inject a property on my class.

export class ADFSRepository extends AuthFacade {
  @Inject
  private readonly dataSource: ADALDataSource
}

The injected value is bound, it is an abstract class, and it works perfectly fine if I force the Inject declaration to always go to the property declaration here https://github.com/thiagobustamante/typescript-ioc/blob/623a58abd16403f134e5556b83043123155b695d/src/decorators.ts#L149-L150 The issue seems to be that my args is an array of length 3, where the third argument is an object.

0: AuthFacade {constructor: ƒ, signInWithUsername: ƒ}
1: "dataSource"
2: {configurable: true, enumerable: true, writable: true, initializer: null}
length: 3

jhd-lego avatar May 19 '20 12:05 jhd-lego

The same issue

aopanasenko avatar Nov 30 '20 17:11 aopanasenko

Same issue here 😞 . Any solutions/workarounds?

danielgelling avatar Jul 13 '21 17:07 danielgelling

Looks like youre not using ts-node (or ts-jest) to execute the code.

This could be related to https://github.com/evanw/esbuild/issues/412 and https://github.com/thiagobustamante/typescript-ioc/issues/77

InsOpDe avatar Aug 21 '21 10:08 InsOpDe

Hi, I have the same issue, and I did some experiments. @Inject is not working when the project is compiled with create-react-app (babel...) but if I use pure ts project created with webpack-cli (ts-loader) without babel it works fine.

I have also found some suggestions to add some bubel plugins such as: "plugins": [ "babel-plugin-transform-typescript-metadata", ["@babel/plugin-proposal-decorators", { "legacy": true }] ] but it doesn't help in my case.

AvetisSargsian avatar Apr 03 '22 12:04 AvetisSargsian

Documentation says that when injecting on a property 2 arguments are given, the class and the property name, and also explains why that is the case IN TYPESCRIPT: https://www.typescriptlang.org/docs/handbook/decorators.html#property-decorators

However, it looks like other interpreters are able to pass also the third parameter. So, to support both cases, the check in decorators.ts line 149 should be different:

if (args.length === 3 && typeof args[2] === 'number') {
  return InjectParamDecorator.apply(this, args);
} else if (args.length >= 2) {
  return InjectPropertyDecorator.apply(this, args);
}
// Case for decorator on a class
throw new TypeError('Invalid @Inject Decorator declaration.');

It would still be missing the case of the decorator used on a method, but would work in react.

SimoneGianni avatar Oct 25 '22 16:10 SimoneGianni