Now, you can also implement the TodoList class:
class TodoList { private _todoList: ReadonlyArray<TodoItem> = []; constructor(todoList?: TodoItem[]) { // first we make sure that we have received a valid array // reference: https://developer.mozilla.org/en-
// US/docs/Web/JavaScript/Reference/Global_Objects
// /Array/isArray if(Array.isArray(todoList) && todoList.length) { this._todoList = this._todoList.concat(todoList); } } get todoList(): ReadonlyArray<TodoItem> { return this._todoList } addTodo(todoItem: TodoItem) { if(todoItem) { // the value is "truthy": // not null, not undefined, not NaN, not an empty string,
// not 0, not false this._todoList = this._todoList.concat(todoItem); } } removeTodo(itemId: string) { if(itemId) { this._todoList = this._todoList.filter(item => { if(item.identifier === itemId) { return false; // drop } else { return true; // keep } }); } } }
Here, we again favor immutability to use a more functional approach.