Hi have a typescript test project with inversify and requirejs that can be cloned from: https://github.com/ahedreville/ioctest.git
His package.json is a follows
{
"name": "ioctest",
"version": "1.0.0",
"description": "Test of an IoC with Inversify and RequireJS",
"scripts": {
"start": "http-server"
},
"author": "Alexandre Hedreville",
"license": "ISC",
"devDependencies": {
"http-server": "^0.10.0"
},
"dependencies": {
"@types/requirejs": "^2.1.30",
"inversify": "^4.3.0",
"reflect-metadata": "^0.1.10",
"requirejs": "^2.3.4"
}
}
You can get this project running on your browser issuing the following commands...
mkdir temp
cd temp
git clone https://github.com/ahedreville/ioctest.git
cd ioctest
c:\temp\ioctest>tree /F /A
...
C:.
| app.main.js
| index.html
| package.json
| README.md
| tsconfig.json
|
\---src
App.js
App.js.map
App.ts
ioc_config.js
ioc_config.js.map
ioc_config.ts
Question.js
Question.js.map
Question.ts
Question2.js
Question2.js.map
Question2.ts
Types.js
Types.js.map
Types.ts
npm install
npm start
http://localhost:8080
As you can see, opening the browser developer console log, the interface IProblem is resolved to Question2 class as we requested to do on class ioc_config.ts
Question2 class is as follows
import { injectable } from "inversify";
import { IProblem } from './Types';
@injectable()
export class Question2 implements IProblem {
public readonly factor: number = 0;
public count: number = 0;
public answer: number = 0;
}
but if I change the target resolution class on ioc_config.ts
changing ioc_config.ts class as follows
import { Container } from 'inversify';
import { sTypes, IProblem } from './Types';
import { Question } from './Question';
import { Question2 } from './Question2';
export const container = new Container();
//container.bind<IProblem>(sTypes.IProblem).to(Question2);
container.bind<IProblem>(sTypes.IProblem).to(Question);
now the resolution of the IProblem interface will fail since Question.ts HAS A CONSTRUCTOR THAT EXPECT SOME VALUES to get passed into.
Question class is as follows
import { injectable } from "inversify";
import { IProblem } from './Types';
@injectable()
export class Question implements IProblem {
public answer: number = 0;
constructor(public readonly factor: number, public readonly count: number) {
}
}
How can I solve this issue (I am newbie on inversify) and pass some values at resolution time to the constructor?