Complete the code to mark the provider as optional in the constructor.
constructor(@[1]() private readonly service?: MyService) {}The @Optional() decorator marks the provider as optional, so NestJS won't throw an error if it is missing.
Complete the code to import the Optional decorator from the correct package.
import { [1] } from '@nestjs/common';
The Optional decorator is imported from @nestjs/common to mark providers as optional.
Fix the error in the constructor to correctly inject an optional provider.
constructor(@Optional() private readonly service?: [1]) {}To inject an optional provider, you must use the @Optional() decorator on the parameter, not change the type. The correct way is to decorate the parameter, not change its type.
Fill both blanks to correctly inject an optional provider with a custom token.
constructor(@[1]('MY_TOKEN') @[2]() private readonly service?: MyService) {}
When injecting a provider by a custom token, use @Inject('TOKEN'). To make it optional, add @Optional() decorator.
Fill all three blanks to create an optional provider with a default value fallback.
const defaultService = [3]; constructor(@[1]() private readonly service?: MyService) { this.service = this.service ?? [2]; }
The @Optional() decorator marks the provider as optional. If it's not injected, the code assigns a default instance new MyService() to defaultService and uses it as fallback.