Skip to content

inject with constructor

Theo Sun edited this page Nov 9, 2020 · 4 revisions

@newdash/inject support create a class instance by decorate with class constructor

it('should support deep constructor injection', async () => {

  class A {
    v: number
    constructor(@inject("v") v) {
      this.v = v;
    }
  }

  class B {
    a: InjectWrappedInstance<A>
    constructor(@inject(A) a) {
      this.a = a;
    }
  }

  const ic = InjectContainer.New();
  ic.registerInstance("v", 999);
  const b = await ic.getInstance(B);

  expect(b.a.v).toBe(999);

});