-
Notifications
You must be signed in to change notification settings - Fork 724
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Failing to @inject into class constructor #1007
Comments
I had the same issue when I was using babel. Use tsloader instead of it! |
so did anybody open an issue in babel project? |
i faced similar issue. after importing |
I was having a similar issue in typescript with optional properties coming after the injected properties 'missing required @Inject on argument 2'. The solution for me was to explicitly assign undefined to the optional properties i.e. constructor(@inject(TYPES.IFetch) updateProvider: IFetch,
@inject(TYPES.ILogger) logger: ILogger,
indexedDb?: IDBFactory, ... constructor(@inject(TYPES.IFetch) updateProvider: IFetch,
@inject(TYPES.ILogger) logger: ILogger,
indexedDb: IDBFactory | undefined = void 0, as a small aside, ideally the error message would state |
I'm on RN 0.59.5, TS 3.4.5, Babel 7.4.4 ( I had to switch to using
|
I have set up a simple repo to illustrate this here: https://github.com/nsainaney/inversifyBabel
|
I was finally able to fix the above. I was missing:
I've updated the codebase here: https://github.com/nsainaney/inversifyBabel. Hope this helps others out |
I've added what @wyzzy suggested and it worked fine.
babel.config.js
metro.config.js
|
For anyone that runs into this, I solved my babel-only solution by using https://github.com/WarnerHooh/babel-plugin-parameter-decorator |
@ryall I think the problem here is just that Inversify doesn't support class member injection directly in constructor. If you want to inject in the constrcutor you have to turn it into normal params by removing the visibility modifier. |
Check the stackoverflow.com::babel-7-inversify-4-webpack-4-unexpected-character-on-inject answer. babel-plugin-transform-typescript-metadata plugin helps. |
I had the same issue using constructor injection in a react app which was created using create-react-app (react 17, react-scripts 4).
(I'm not sure if all of those dev-dependencies are required though) 2- update package.json build and start scripts:
3- add a file called config-overrides.js at the same level as package.json which includes the following:
you can read about it here |
I used inversify and mobx in my project. I also use expo and all possible expo modules. This solution eliminates the problem, but leads to many other problems that will emerge later and all of them will be related to expo. I had to abandon the react-native-typescript-transformer in favor of the default metro transformer. Therefore, if you use expo or are going to use it, I strongly advise you not to use react-native-typescript-transformer. Otherwise, unexpected surprises may await you in the future. |
In my project, I had similar issues caused by babel and inversify You can find my detailed answer here >> https://stackoverflow.com/a/78696852/13607767 |
After reading the comments, I understand this is not an inversify issue. I'm closing it for now, feel free to ping me if I'm mistaken. |
@notaphplover @ryall only field injection worked in react ts: @inject("INetworkManager")
private networkManager!: INetworkManager;
// constructor(
// @inject("INetworkManager") private networkManager: INetworkManager,
// ) {} However, I also have next ts application and constructor injection works as intended. I am from backend/mobile area, and in Spring boot for example field injection is not recommended way. The recommended injection is construction injection, but since, i did not find any solution, I used field injection |
Hey @ryall, the thing is, this is probably an issue in the transpiler / bundler you're using. If property decorators are working and parameter decorators not, is there any chance legacy decorators are not properly transpiled? Can you try to do manually a parameter injection via |
I believe I have everything set up correctly but
@inject
is failing to inject into the class constructor with the error:Missing required @inject or @multiInject annotation in: argument 0 in class AccountFactory
despite being defined on both properties.Using
@inject
on class properties works as expected. I'm only having this problem with constructor params.Expected Behavior
This should inject the correct service
Current Behavior
I get the error:
Missing required @inject or @multiInject annotation in: argument 0 in class AccountFactory
.Types are definitely correct.
Property injection works as expected:
Possible Solution
Maybe I have configured something incorrectly, so here are the relevant files:
Inversify container definition:
tsconfig.json
.babelrc:
webpack.js:
Context
Without this, I cannot isolate units for testing without relying on Inversify rebinds.
Your Environment
Inversify: 5.0.1
reflext-metadata: 0.1.12
The text was updated successfully, but these errors were encountered: