-1

I'm trying to display json data in my angular material table but I have a problem I don't understand.

error: The type argument '{type: string; weight: number; }' is not assignable to the type parameter 'never'.

What does type never mean ?

ts.file

export class TableComponent implements OnInit {

  ELEMENT_DATA: Itype[] = [];
  displayedColumns: string[] = ['dechets.type', 'dechets.weight'];
  dataSource = new MatTableDataSource<any>(this.ELEMENT_DATA);

  constructor(private typeService: TypeService) { }

  ngOnInit() {
    this.getAllDetails();
  }

  public getAllDetails() {
    let resp = this.typeService.getAllType();
    resp.subscribe(resp => {
      this.dataSource.data = resp.reduce((acc, item) => {
        acc.push(...item.dechets); // probleme is here
        return acc;
      }, []);
    });
  }

interface

export interface Itype {

    type_collecte: string;
    dechets:
    {
        type: string;
        weight: number
    }[];
    code_traitement: string;
    annee: number;
    mois: number;
}
Vinodea Richard
  • 31
  • 1
  • 11
  • 1
    try defining a type for acc: IType[] – mak15 Apr 27 '21 at 11:12
  • 2
    Does this answer your question: [What is “not assignable to parameter of type never” error in typescript?](https://stackoverflow.com/q/52423842/8017690)? – Yong Shun Apr 27 '21 at 11:12

1 Answers1

0

If you're looking to collect all the dechets arrays from each element of the resp array and flatten them, you could use Array#map with Array#flat.

this.typeService.getAllType().subscribe(resp => {
  this.dataSource.data = resp
    .map(item => item.dechets)
    .flat(1);
});
ruth
  • 29,535
  • 4
  • 30
  • 57