У меня ошибка при компиляции проекта.
Терминал ошибки:
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'.
Вот код, в котором выбита ошибка.
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 }
Вот ошибка в терминале, когда я внес изменения в код.
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'.
Подскажите, как исправить эту ошибку, буду очень признателен.