编译项目角度时发生错误,错误TS2322

时间:2018-06-23 11:53:42

标签: angular angular5 angular-cli

在我编译项目时,这样的错误就消失了:

Date: 2018-06-23T07:09:31.526Z
Hash: 850794f8281827da9d4b
Time: 4886ms
chunk {main} main.js, main.js.map (main) 2.15 kB [initial] [rendered]
chunk {runtime} runtime.js, runtime.js.map (runtime) 5.22 kB [entry] [rendered]
chunk {styles} styles.js, styles.js.map (styles) 10.3 kB [initial] [rendered]
chunk {vendor} vendor.js, vendor.js.map (vendor) 326 kB [initial] [rendered]

ERROR in src/app/services/task.ts(46,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'.

ℹ 「wdm」: Failed to compile.

发誓要这段代码:

42    create(task): Promise<Task> {
43      task['due_date'] = task['due_date']['formatted'];
44      let body = JSON.stringify({task: task});
45      const url = `${this.tasksUrl}?access_token=${localStorage.getItem('token')}`;
46      return this.http.post(url, body, { headers: this.headers })
47       .toPromise()
48       .then(res => res.json() as Task)
49       .catch(error => {
50        this.handleError(error, 'Could not create task!')
51      });
52    }

告诉我如何解决此错误?谢谢。

0 个答案:

没有答案