Ewan
Ewan

Reputation: 548

Angular Component with Generic Type

I have a component (table) that takes an array of any data. This component then calls a function in a service that requires a generic to determine what the data is (the function maps data).

However, I don't know of a way to using a generic with an Input / Component.

Anyone know how to do this?

When the component is used (in the template) on the parent level I would like to provide the actual Type of T. Any ideas?

@Component({
  selector: 'inm-mapped-table',
  template: `
  <pre>{{ $rows | async }} </pre>
  `,
})
export class InmMappedTableComponent<T extends Record<string, any>> implements OnInit {
  @Input() $data: Observable<T[]>;
  @Input() config: MappingConfig<T>[] = [];

  $rows: Observable<Row[]>;

  constructor(private readonly mappingService: InmTableMappingService) {}

  ngOnInit(): void {
    this.$rows = this.$data.pipe( 
        switchMap( data => this.mappingService.getRows<T>(data, this.config) )
    )
  }
}

Upvotes: 1

Views: 1628

Answers (0)

Related Questions