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

Explanation

Register { provide: PAYMENT_GATEWAY, useClass: StripeGateway } in production and a fake in tests.

Further reading

Problem with a question

NestJS · Middle · question 13

Test yourself

Take the NestJS · Middle quiz: questions and options are shuffled, and you get a review of your mistakes at the end.

Take this quiz