Typescript have generics class, but of course you can't do generics components in Angular. You have to break type safety if you want to do something re-usable.

  • 1
    Just compare the objects. Classes are just a contrived way to create prototype-linked objects. Don't treat TS or JS as class oriented because they are not. If you are stonewalling, then it wasn't designed that way.
  • 0
    @theuser It's not class-oriented originally but it's (badly) enforced by Angular. The TemplateRef is a generic for something, but I have two choices - using any, or making my own component generic, whiiiiich, no, I can't.

    It's overall bad design.
  • 1
    components typed against an interface + component factory that does the magic a emit the correct implementation. in templates you instanciate only the factory then.
    it’s a workaround but it doest the job. finally it’s a classic strategy pattern.

    otherwise, components are able to figure out the generic type on @Input, you generic logic can be implemented starting from here. class declaration can stit happily untyped.

    unfortunately angular needs to stick with this class-ish approach, otherwise ex-javaers feel lost going for what TS actually is, functional with structural type system rather then classic OOP + nominal types 😂
Add Comment