Back to all reviewers

avoid @Optional decorator

novuhq/novu
Based on 2 comments
TypeScript

Avoid using the @Optional decorator in NestJS dependency injection as it disables runtime DI validation during bootstrap. This can cause missing provider dependencies to go undetected during compilation, leading to runtime failures without clear error messages.

NestJS TypeScript

Reviewer Prompt

Avoid using the @Optional decorator in NestJS dependency injection as it disables runtime DI validation during bootstrap. This can cause missing provider dependencies to go undetected during compilation, leading to runtime failures without clear error messages.

Instead of using @Optional, prefer TypeScript optional properties with the ? syntax to maintain type safety while preserving NestJS’s ability to validate dependencies at startup.

Example:

// Avoid - disables DI validation
constructor(
  @Optional()
  private sendWebhookMessage: SendWebhookMessage
) {}

// Prefer - maintains DI validation
constructor(
  private sendWebhookMessage?: SendWebhookMessage
) {}

This approach ensures that dependency injection issues are caught early in the development cycle rather than surfacing as obscure runtime errors in production.

2
Comments Analyzed
TypeScript
Primary Language
NestJS
Category

Source Discussions