In my model class, the structure looks like this:
export class Task {
public name: string;
public status: string = "todo";
public completeTask(): void {
this.status = "done";
}
}
There is also a service responsible for retrieving tasks:
export class TaskService {
constructor(private _http: Http) {}
public getTask(): Observable<Task> {
return this._http
.get("url")
.map(res => res.json().data as Task)
}
}
However, when attempting to execute the completeTask
function on the task object, an error message occurs:
TypeError: task.completeTask() is not a function
The same error is encountered when trying to convert a JSON literal object to a task instance.
let task: Task = <Task>{ name: "Make some coffee" }
task.completeTask(); // This line triggers an error
I am wondering if I made a mistake somewhere. How can I ensure that functions are properly included in my task objects?