Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

TypeDI @Inject() doesn't work, but Container.get() does

I'm facing weird issue, Container.get(MyServiceName); returns demanded service, yet class property decorated with @Inject() is undefined.

I've tried decorate class with @Service()

I do import reflect-metada at entry point of my application import 'reflect-metadata';

I'm wondering if it may be related to fact, that that I'm using typedi not directly from my node_modules but from my dependency?

Application doesn't use Express framework neither routing controllers

My tsconfig.json:

"compilerOptions": {

    "declaration": true,
    "pretty": true,
    "esModuleInterop": true,
    "target": "esnext",
    "noImplicitAny": true,
    "noImplicitReturns": true,
    "noUnusedLocals": false,
    "moduleResolution": "node",
    "noUnusedParameters": true,
    "strictPropertyInitialization": false,
    "module": "commonjs",
    "lib": ["dom", "es2018"],
    "importHelpers": true,
    "outDir": "./dist",
    "strict": true,
    "typeRoots": ["node_modules/@types"],
    "experimentalDecorators": true,
    "emitDecoratorMetadata": true,
    "sourceMap": true
  },
  "include": ["./src/**/*"],
  "exclude": ["node_modules", "./dist/**/*"]
}

Service that I want to be injected

import MyServiceName from '../services/MyServiceName';
import { Container, Inject, Service } from 'my_dependency/lib';

export default class SomeClass{
  @Inject()
  private readonly someService: MyServiceName;

  public async someMethod(): SomeResponseType {
    const thatWorks = Container.get<MyServiceName>(MyServiceName);
    console.log(this.someService); // undefined
    console.log(thatWorks); // proper class
  }
}

Service I want to inject

@Service()
export default class MyServiceName {
  public async someMethod(): SomeReturnType {
    return someReturnedData;
  }
}

I would like to inject my dependencies though @Inject() decorator

like image 661
Łukasz Śmiałowski Avatar asked Sep 01 '25 04:09

Łukasz Śmiałowski


1 Answers

You need to create your instance of SomeClass using Container#get, or the container won't be able to inject the property.

This worked for me:

// a.ts
import { Service } from "typedi";
@Service()
export class Foo {
  foo() {
    return 10;
  }
}
// b.ts
import { Foo } from "./a";
import { Inject } from "typedi";

export class Bar {
  @Inject() private readonly service: Foo
  foo() {
    console.log(this.service);
  }
}
// test.ts
import "reflect-metadata";
import { Bar } from "./b";
import { Container } from "typedi";

const noFoo = new Bar();
noFoo.foo(); // yields undefined

const withFoo = Container.get<Bar>(Bar);
withFoo.foo(); // yields Foo
like image 140
michaeln Avatar answered Sep 02 '25 17:09

michaeln