Let's say I got a component with a fixed input parameter type,
@Component({ selector: 'fixed', template: '<div>{{value}}</div>' }) export class FixedComponent { @Input() value: string; }
How do I go about making that parameter type generic, i.e.
@Component({ selector: 'generic', template: '<div>{{value}}</div>' }) export class GenericComponent<T> { @Input() value: T; }
That is, how do I pass the type in the template of the parent component?
<generic ...></generic>
@Input() and @Output() give a child component a way to communicate with its parent component. @Input() lets a parent component update data in the child component. Conversely, @Output() lets the child send data to a parent component.
In TypeScript, generics can be used to better describe the types in classes that are reusable with multiple derived types. Angular does not support instantiation of generic components, but this limitation can be worked around by creating a derived component for each derived type we want to use it with.
It seems that when using AOT compilation the only way to do this is to replace the generic type with 'any'. See https://github.com/angular/angular/issues/11057
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With