You signed in with another tab or window. Reload to refresh your session.You signed out in another tab or window. Reload to refresh your session.You switched accounts on another tab or window. Reload to refresh your session.Dismiss alert
Describe the bug
While doing some unit test to evaluate the library I found that a simple class registration throws TypeInfo not Known only when it has construction parameters. It works when no construction parameters.
I must be missing something, but I already added
reflect-metadata
decorators
To Reproduce
create the following jest unit test
launch
line const papa = container.resolve(Papa); --> works because Papa class has no parameters
line const foo = container.resolve<Toto>(Toto); -> fails with TypeInfo not know
replace Toto class constructor with an empty one -> it works
By the way, I also tried having Toto constructor with only Bar parameter and still fails (in case primitive type needed to be handled differently)
import'reflect-metadata';import{container,injectable}from'tsyringe';beforeEach(()=>{container.reset();container.clearInstances();});interfaceIToto{getName(): string;}
@injectable()classBar{}
@injectable()classPapaimplementsIToto{getName(): string{return'papa';}}
@injectable()classTotoimplementsIToto{name: string='default';//constructor() {constructor(name: string,privatevalue: Bar){console.log(`TOTO instance created with name '${name}' and value '${value}'`);}publicgetName(): string{returnthis.name;}}describe('tsyringe bug ?',()=>{test('constructor with parameters generates typeInfo not known',()=>{container.registerInstance(Bar,newBar());//container.register<IToto>('ITotot', { useToken: Toto });container.register(Toto,{useClass: Toto});container.register(Papa,{useClass: Papa});container.registerInstance(String,'toto');constpapa=container.resolve(Papa);constfoo=container.resolve<Toto>(Toto);});});
Expected behavior
The instruction container.register(Toto, { useClass: Toto }); should be enough to create the related TypeInfo
Version:
"tsyringe": "^4.7.0"
The text was updated successfully, but these errors were encountered:
Given everything I've read about TSyringe, the solution is to inject each of your parameters. This sort of makes sense, given that in order for a class to be injectable, TSyringe needs to know how to fulfill each parameter in the constructor. In this case, you'd have to @inject('someThingForName') name: string etc for each of your parameters.
If you don't want to have the params be injected, you can instead use container.register('TotoClass', {useValue: new Toto(... inject yourself)}) or container.register('TotoClassType', {useValue: Toto})
Describe the bug
While doing some unit test to evaluate the library I found that a simple class registration throws TypeInfo not Known only when it has construction parameters. It works when no construction parameters.
I must be missing something, but I already added
To Reproduce
const papa = container.resolve(Papa);
--> works becausePapa
class has no parametersconst foo = container.resolve<Toto>(Toto);
-> fails withTypeInfo not know
Toto
class constructor with an empty one -> it worksBy the way, I also tried having
Toto
constructor with onlyBar
parameter and still fails (in case primitive type needed to be handled differently)Expected behavior
The instruction
container.register(Toto, { useClass: Toto });
should be enough to create the relatedTypeInfo
Version:
"tsyringe": "^4.7.0"
The text was updated successfully, but these errors were encountered: