Nest.js Injector decorator doesn't seem to register service when called manually

Viewed 121

I am dynamically creating a nest js service class:

const generatedClass = {
   [className]: class { ... }
}[className]

I wish to mimic the usual function of

@Injectable()
export class MyService { }

And as such, I am doing:

const generatedClass = {
   [className]: class { constructor(private dep1: Dep1...) {} }
}[className]

Injectable()(generatedClass)

to mimic the decorator registering the class. However, when doing this, the service does not seem to get reigstered with the DI container, and when I inject this service, it doesn't seem to resolve the dependencies in the constructor.

The service seems to inject correctly, but it does not resolve the dependencies inside the service, leaving me quite confused. Is it possible to call Injectable manually or am I missing some steps?

1 Answers

Looking into this, even in Typescript's playground this is the same behavior. There's just no way to call a decorator manually and get Typescript to emit the metadata. You'll need to add some sort of decorator, be it @Optional() or @Inject() to get Typescript to output the metadata properly

Related