NestJS · Middle · Question 13 of 75
Why inject an interface-like dependency by token instead of by class?
@Inject(PAYMENT_GATEWAY) private readonly payments: PaymentGateway- 1Tokens make injection faster
- ✓Correct answer: TypeScript interfaces don't exist at runtime, so a token (string or symbol) is needed to swap implementations
- 3Classes can't be injected
Explanation
Register { provide: PAYMENT_GATEWAY, useClass: StripeGateway } in production and a fake in tests.