Config injection improvement
Given config like this:
export class HelloConfig {
title: string = 'World'
color: string = 'yellow'
year: number = 2022
}
export class Config {
hello: HelloConfig = new HelloConfig()
}
Currently injecting works this way:
@cli.controller('test')
export class TestCommand implements Command {
constructor(
protected config: Config['hello'],
) {}
}
But it doesn't work this way, although HelloConfig and Config['hello'] is the same type:
@cli.controller('test')
export class TestCommand implements Command {
constructor(
protected config: HelloConfig,
) {}
}
Will give me this error:
DependenciesUnmetError: Undefined dependency "config: any" of TestCommand(?). Type has no provider in scope cli.
Can we support injecting directly using HelloConfig like the above code? Otherwise, suppose we have a deeply nested config, we will have to write something like Config['fieldA']['fieldB']['fieldC'] to inject the needed config class.
This config module for Nest.js is an example: https://github.com/Nikaple/nest-typed-config
It supports injecting any sub-config:
@Injectable()
export class AppService {
// inject any config or sub-config you like
constructor(
private config: RootConfig,
private databaseConfig: DatabaseConfig,
private tableConfig: TableConfig,
) {}
}
The configuration injection needs always a reference to the real used configuration class. So, by defining a dependency protected config: HelloConfig where HelloConfig is not the configuration object, the system is not able to inject it as there is no relation known to Config. You have to use the notation protected config: Config['hello'], instead.
I'm not sure yet if would be easily possible to support that case, but currently its not due to design limitation. We could try it though as it makes sense.