#Does NestJS natively supports automatic base-class injection ?

2 messages · Page 1 of 1 (latest)

brisk leaf
#

ex:


@Injectable()
export class AzurePartnerCenterStrategy
  extends BasePartnerCenterCartStrategy
  implements PartnerCenterStrategy
{
  constructor(protected readonly partnerCenterApiService: PartnerCenterApiService) {
    super(partnerCenterApiService);
  }
}

and the BasePartnerCenterCartStrategy

export abstract class BasePartnerCenterCartStrategy {
  constructor(protected readonly partnerCenterApiService: PartnerCenterApiService) {}

As you can see in the AzurePartnerCenterStrategy i need the constructor eventhough the PartnerCenterApiService is already instantiated in the BasePartnerCenterCartStrategy which is extended by the AzurePartnerCenterStrategy!

Why do i need that constructor, it seems redundant? (if i skip it it give me error - undefined)

knotty hornet
#

Two things.

  1. It's TypeScript and JavaScript and inheritance isn't like with regular class based languages, like Java. Learn about prototypical inheritance in JavaScript and then you'll get why you need the extra constructor. Long story short, the base class isn't a base, but the root.

  2. If you called it ParentPartnerCenterCartStrategy, it would actually be more in line with the hierarchical inheritance standpoint of JavaScript. That might not help you directly get the understanding you are looking for, but it might. 😊