0
0
NestJSframework~20 mins

Optional providers in NestJS - Practice Problems & Coding Challenges

Choose your learning style9 modes available
Challenge - 5 Problems
🎖️
NestJS Optional Provider Master
Get all challenges correct to earn this badge!
Test your skills under time pressure!
component_behavior
intermediate
2:00remaining
What happens when an optional provider is not registered?
In NestJS, if a service is injected as an optional provider but is not registered in the module, what will be the value of the injected dependency inside the consuming class?
NestJS
import { Injectable, Optional } from '@nestjs/common';

@Injectable()
export class ConsumerService {
  constructor(@Optional() private readonly optionalService?: OptionalService) {}

  getStatus() {
    return this.optionalService ? 'Service available' : 'Service missing';
  }
}

// Assume OptionalService is NOT provided in any module
AThe constructor will not be called because of missing provider
B'Service available' because NestJS creates a default instance automatically
CThrows a runtime error due to missing provider
D'Service missing' because the optional provider is undefined when not registered
Attempts:
2 left
💡 Hint
Think about what @Optional() decorator does when the provider is missing.
📝 Syntax
intermediate
2:00remaining
Identify the correct syntax to mark a provider as optional in NestJS
Which of the following constructor parameter declarations correctly marks a provider as optional in NestJS?
NestJS
import { Injectable, Optional } from '@nestjs/common';

@Injectable()
export class ExampleService {
  constructor( /* ??? */ ) {}
}
Aconstructor(@Optional() private readonly service: SomeService) {}
Bconstructor(private readonly service: @Optional() SomeService) {}
Cconstructor(private readonly @Optional() service: SomeService) {}
Dconstructor(private readonly service?: SomeService) {}
Attempts:
2 left
💡 Hint
Look at how decorators are placed before parameters in TypeScript.
state_output
advanced
2:00remaining
What is the output when an optional provider is injected and used?
Given the following NestJS service and module setup, what will be the output of the method call consumer.getMessage()?
NestJS
import { Injectable, Optional } from '@nestjs/common';

@Injectable()
export class OptionalService {
  getMessage() {
    return 'Hello from OptionalService';
  }
}

@Injectable()
export class ConsumerService {
  constructor(@Optional() private readonly optionalService?: OptionalService) {}

  getMessage() {
    return this.optionalService?.getMessage() ?? 'No service available';
  }
}

// Module setup:
// @Module({
//   providers: [ConsumerService] // OptionalService is NOT provided
// })
A'Hello from OptionalService'
BThrows a runtime error because optionalService is undefined
C'No service available'
DReturns undefined
Attempts:
2 left
💡 Hint
Check how the optionalService is used with optional chaining and fallback.
🔧 Debug
advanced
2:00remaining
Why does this optional provider injection cause a runtime error?
Consider this NestJS service constructor:
constructor(@Optional() private readonly service: SomeService) {}
If SomeService is not provided in any module, why might this code still cause a runtime error?
NestJS
import { Injectable, Optional } from '@nestjs/common';

@Injectable()
export class ConsumerService {
  constructor(@Optional() private readonly service: SomeService) {
    console.log(this.service.getName());
  }
}
ABecause service is undefined and calling getName() on undefined causes a runtime error
BBecause @Optional() decorator is missing
CBecause NestJS always throws error if provider is missing regardless of @Optional()
DBecause the constructor is missing the @Inject decorator
Attempts:
2 left
💡 Hint
Think about what happens when you call a method on undefined in JavaScript.
🧠 Conceptual
expert
2:00remaining
How does NestJS resolve optional providers during dependency injection?
Which statement best describes how NestJS handles optional providers when resolving dependencies?
ANestJS throws an error if the provider is not registered, even if marked optional
BNestJS injects undefined for optional providers if they are not registered, allowing the consumer to handle absence gracefully
CNestJS injects a default instance of the provider if it is not registered, ensuring no errors occur
DNestJS skips the constructor parameter entirely if the provider is optional and missing
Attempts:
2 left
💡 Hint
Consider what @Optional() means for dependency injection behavior.