简体   繁体   中英

Angular, Type 'Promise<void | Task>'

I have this error when compiling a project. error terminal:

ERROR in src/app/services/task.ts(44,5): error TS2322: Type 
'Promise<void | Task>' is not assignable to type 'Promise<Task>'.
Type 'void | Task' is not assignable to type 'Task'.
Type 'void' is not assignable to type 'Task'.

Here is the code in which the error is knocked out.

26 loadTasks(): Promise<Task[]> {
27 const url = `${this.tasksUrl}?access_token=${localStorage.getItem('token')}`;
28 return this.http.get(url)
29   .toPromise()
30 .then(res => res.json() as Task[])
31  .catch(error => this.handleError(error, 'Could not load tasks!'));
32 }
33 getTask(id: number): Promise<Task> {
34 const url = `${this.tasksUrl}/${id}?access_token=${localStorage.getItem('token')}`;
35 return this.http.get(url)
36  .toPromise()
37  .then(res => res.json() as Task)
38  .catch(error => this.handleError(error, 'Could not load task!'));
39 }
40 create(task): Promise<Task> {
41 task['due_date'] = task['due_date']['formatted'];
42 let body = JSON.stringify({task: task});
43 const url = `${this.tasksUrl}?access_token=${localStorage.getItem('token')}`;
44 return this.http.post(url, body, { headers: this.headers })
45  .toPromise()
46  .then(res => res.json() as Task)
47  .catch(error => {
48    this.handleError(error, 'Could not create task!')
49  });
50 }
51
52  update(task) {
53 const url = `${this.tasksUrl}/${task.id}?access_token=${localStorage.getItem('token')}`;
54 if (task['due_date'] && task['due_date']['formatted']) {
55  task['due_date'] = task['due_date']['formatted'];
56 }
57 return this.http.put(url, JSON.stringify(task), { headers: this.headers })
58  .toPromise()
59  .then(res => res.json() as Task)
60  .catch(error => {
61    this.handleError(error, 'Could not update task!')
62  });
63 }

Here is the error in the terminal when I made changes to the code.

create(task): Promise<Task | void> 

error terminal: 
ERROR in src/app/components/tasks/form.ts(62,26): error TS2345: Argument of type 'void | Task' is not assignable to parameter of type 'Task'.
Type 'void' is not assignable to type 'Task'.

Tell me how to fix this error, I will be very grateful.

Recently I was facing the same issue after I updated my typescript to version 2.7.2

I fixed it by making the changes to catch block. My errorHandler() throws exception Observable.throw which is of type ErrorObservable

I simply chained the Observable.throw from my custom method which was calling errorHandler() . In your case it is create()

You could cimply do this

create(task): Promise<Task> {
    task['due_date'] = task['due_date']['formatted'];
    let body = JSON.stringify({task: task});
     const url = `${this.tasksUrl}?access_token=${localStorage.getItem('token')}`;
     return this.http.post(url, body, { headers: this.headers })
      .toPromise()
      .then(res => res.json() as Task)
      .catch(error => {
       Observable.throw(this.handleError(error, 'Could not create task!')); // chain Observable.throw
    });
}

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM