All quizzesHard
DI Container Internals — Series 2
Preview — 3 of 10 questions
design:paramtypes for this constructor is [ConfigService, Object]. How does Nest still resolve the second argument?
javascript
@Injectable()
export class ReportService {
constructor(
private readonly config: ConfigService,
@Inject('PDF_ENGINE') private readonly engine: unknown,
) {}
}A@Inject() stores the token under a separate metadata key, keyed by parameter index; the injector merges those self-declared dependencies over the emitted design:paramtypes, so index 1 resolves to 'PDF_ENGINE' instead of Object
BNest scans the module's providers for one whose instance is assignable to unknown and picks it
C@Inject() rewrites the class's design:paramtypes array in place at decoration time
DThe parameter name engine is read from the function source and matched against provider tokens
What is true of this form compared with constructor injection?
javascript
@Injectable()
export class ReportService {
@Inject(ConfigService) private readonly config: ConfigService;
}AIt is resolved lazily, on first property access
BIt is the only form that works inside abstract base classes
CIt works — the injector collects property dependencies from their own metadata key and assigns them after instantiation — but the field is unset during the constructor, and the dependency is easier to overlook when reading or testing the class
DIt is rejected at startup; Nest supports constructor injection only
Why does the injector see nothing usable?
javascript
@Injectable()
export class NotifierService {
constructor(private readonly channel: EmailChannel | SmsChannel) {}
}
// Nest can't resolve dependencies of the NotifierService (?).ABecause union types are forbidden in a Nest provider's constructor
BBecause TypeScript cannot emit a single runtime value for a union type, so design:paramtypes records Object — a token nothing is registered under, which surfaces as the unresolvable ?
CBecause both classes are registered, and the injector refuses to choose between two candidates
DBecause emitDecoratorMetadata skips any parameter whose type is declared with private readonly
Sign up free to play
Answer all 10 questions (7 more), see explanations for every answer, and track your score.