错误 TS2769:没有与此调用匹配的过载 Overload 1 of 5

时间:2021-01-09 00:56:30

标签: angular typescript

我不断收到这个奇怪的错误。我之前问过这个问题,但过去的解决方案并没有解决这个问题。我已经附上了关于这个问题的所有编辑过的代码,可以根据要求发布更多。我是 angular 新手,所以请多多包涵。谢谢!

错误信息

Error: src/app/pages/task-view/task-view.component.ts:27:60 - error TS2769: No overload matches this call.
      Overload 1 of 5, '(observer?: NextObserver<Task> | ErrorObserver<Task> | CompletionObserver<Task> | undefined): Subscription', gave the following 
error.
        Argument of type '(tasks: Task[]) => void' is not assignable to parameter of type 'NextObserver<Task> | ErrorObserver<Task> | CompletionObserver<Task> | undefined'.
          Property 'complete' is missing in type '(tasks: Task[]) => void' but required in type 'CompletionObserver<Task>'.
      Overload 2 of 5, '(next?: ((value: Task) => void) | undefined, error?: ((error: any) => void) | undefined, complete?: (() => void) | undefined): Subscription', gave the following error.
        Argument of type '(tasks: Task[]) => void' is not assignable to parameter of type '(value: Task) => void'.
          Types of parameters 'tasks' and 'value' are incompatible.
            Type 'Task' is missing the following properties from type 'Task[]': length, pop, push, concat, and 26 more.
    
    27         this.taskService.getTasks(params.listId).subscribe((tasks: Task[]) => {
                                                                  ~~~~~~~~~~~~~~~~~~~~
    
      node_modules/rxjs/internal/types.d.ts:64:5
        64     complete: () => void;
               ~~~~~~~~
        'complete' is declared here.
    src/app/pages/task-view/task-view.component.ts:34:42 - error TS2769: No overload matches this call.
      Overload 1 of 5, '(observer?: NextObserver<List> | ErrorObserver<List> | CompletionObserver<List> | undefined): Subscription', gave the following 
error.
        Argument of type '(lists: List[]) => void' is not assignable to parameter of type 'NextObserver<List> | ErrorObserver<List> | CompletionObserver<List> | undefined'.
          Property 'complete' is missing in type '(lists: List[]) => void' but required in type 'CompletionObserver<List>'.
      Overload 2 of 5, '(next?: ((value: List) => void) | undefined, error?: ((error: any) => void) | undefined, complete?: (() => void) | undefined): Subscription', gave the following error.
        Argument of type '(lists: List[]) => void' is not assignable to parameter of type '(value: List) => void'.
          Types of parameters 'lists' and 'value' are incompatible.
            Type 'List' is missing the following properties from type 'List[]': length, pop, push, concat, and 26 more.
    
    34     this.taskService.getList().subscribe((lists:List[]) => {
                                                ~~~~~~~~~~~~~~~~~~~
    
      node_modules/rxjs/internal/types.d.ts:64:5
        64     complete: () => void;
               ~~~~~~~~
        'complete' is declared here.

task-view.component.ts

import { Component, OnInit } from '@angular/core';
import { ActivatedRoute, Params } from '@angular/router';
import { TaskService } from 'src/app/task.service';
import { MatDialog } from '@angular/material/dialog';
import { Task } from 'src/app/models/task.model';
import { List } from 'src/app/models/list.model';




@Component({
  selector: 'app-task-view',
  templateUrl: './task-view.component.html',
  styleUrls: ['./task-view.component.scss']
})
export class TaskViewComponent implements OnInit {

  lists?: List[];
  tasks?: Task[];

  constructor(private taskService: TaskService, private route: ActivatedRoute, private dialog: MatDialog) { }

  ngOnInit(): void {
    this.route.params.subscribe(
      (params: Params) => {
        //console.log(params);
        this.taskService.getTasks(params.listId).subscribe((tasks: Task[]) => {
          this.tasks = tasks;
        })
      }

    )

    this.taskService.getList().subscribe((lists:List[]) => {
      console.log(lists);
      this.lists = lists;
    })
  }

  onTaskClick(task: Task) {
    // We want to set the task to completed
    this.taskService.completed(task).subscribe(() => {
      console.log("Completed successfully");
    })
  }


  


}

task.service.ts

import { Injectable } from '@angular/core';
import { Observable } from 'rxjs';
import { List } from './models/list.model';
import { Task } from './models/task.model';
import { WebRequestService } from './web-request.service';

@Injectable({
  providedIn: 'root'
})
export class TaskService {



  constructor(private webReqService: WebRequestService) { // this line is injecting the web-request service we created

   }

  createList(title: string): Observable<List> {
    // We want to send a web request to create a List
    return this.webReqService.post('lists', { title }); // this is returning a observable
  }

  getList(): Observable<List>{
    return this.webReqService.get('lists'); 
  }
  
  getTasks(listId: string): Observable<Task> {
    return this.webReqService.get(`lists/${listId}/tasks`);
  }
  
  createTask(title: string, listId: string): Observable<Task> {
    // We want to send a web request to create a Task
    return this.webReqService.post(`lists/${listId}/tasks`,{ title }); // this is returning a observable
  }

  completed(task: Task): Observable<Task> {
    return this.webReqService.patch(`lists/${task._listId}/tasks/${task._id}`,{
      completed: true // maybe could be set to !completed?
    } )
  }
  

}

1 个答案:

答案 0 :(得分:1)

服务中的 getTasks 函数返回 ,但您试图在组件中获取类型 ,这是错误的。

您必须匹配类型。