DumbComponent abstract class

PHOTO EMBED

Thu Feb 23 2023 15:07:45 GMT+0000 (Coordinated Universal Time)

Saved by @mtommasi #typescript

export abstract class DumbComponent {

   private readonly subClassConstructor: Function;

   protected constructor() {
      this.subClassConstructor = this.constructor;

      if (this.isEmptyConstructor() || arguments.length !== 0) {
         this.throwError('it should not inject services');
      }
   }

   private isEmptyConstructor(): boolean {
      return this.subClassConstructor.toString().split('(')[1][0] !== ')';
   }

   private throwError(reason: string): void {
      throw new Error(`Component "${this.subClassConstructor.name}" is a DumbComponent, ${reason}.`);
   }
}
content_copyCOPY

https://generic-ui.com/blog/enterprise-approach-to-the-smart-and-dumb-components-pattern-in-angular