Robin Singh
Robin Singh

Reputation: 1796

How to fix type is missing from following property in angular?

I worked for the past 2 years on angular, but this is the first time I am using the interface. I face a small issue added below.

Type 'MatTableDataSource<Tasks[]>' is missing the following properties from type 'Tasks[][]': length, pop, push, concat, and 24 more.ts(2740)

Please help me to solve this small issue. Below is my code.

export interface Tasks {
  id: string;
  title: string;
  description: string;
  assignedTo: Array<Team>,
  status: string;
}

Service

 getTasks(): Observable<Tasks[]> {
    return this.http.get<Tasks[]>(`${environment.API_URL}tasks`).pipe(
      map(data => {
        return data;
      })
    )
  }

component file

getTasks() {
    this.taskService.getTasks().subscribe(response => {
      // this.tasks = response;
      console.log(response)
      if (this.tasks) {
        this.dataSource.data = new MatTableDataSource<Tasks[]>(response)
      }
      console.log(this.tasks)
    })
  }

enter image description here

Any solution is appreciated!

Upvotes: 0

Views: 11525

Answers (3)

浩然大人
浩然大人

Reputation: 11


public dataSource!: MatTableDataSource<Tasks>

dataSource.data = response

Upvotes: 1

hawks
hawks

Reputation: 941

You don't need to set the type of MatTableDataSource as Tasks[]. Internally MatTableDataSource treats your type as T[].

dataSource = new MatTableDataSource<Tasks>() // Tasks represents each row of the mat-table

getTasks() {
  // your code
  this.dataSource.data = response
}

I think you should rename your interface to Task and not Tasks. Because it represents only one Task.

Upvotes: 3

Shyam Joshi
Shyam Joshi

Reputation: 923

Add this way -

this.dataSource = new MatTableDataSource<Tasks>(response)

Upvotes: 1

Related Questions