Creating the TodoItem class

Remove everything from the todo-it.ts file. Then, add the definition of the TodoItem class:

class TodoItem { 
    private readonly _creationTimestamp: number;
private readonly _identifier: string; constructor(private _description: string, identifier?: string) { this._creationTimestamp = new Date().getTime(); if (identifier) { this._identifier = identifier; } else { // this is just for the example; for any real project, use // UUIDs instead: https://www.npmjs.com/package/uuid this._identifier = Math.random().toString(36).substr(2,
9); } } get creationTimestamp(): number { return this._creationTimestamp; } get identifier(): string { return this._identifier; } get description(): string { return this._description; } }

We are using encapsulation in order to protect the integrity of our data model (by making the different fields private), and we only create accessors for the fields that we want to expose to the outside. Notice that in the constructor of the class, we initialize a creation timestamp and an identifier (if it wasn't provided).

Creating a TodoItem instance is easy: const todo: TodoItem = new TodoItem("Do the laundry");

If you implement the TodoItem class as we propose, instances of the class will be immutable: Once an instance has been created, you cannot modify it, whether from the inside or from the outside. This is guaranteed because the fields are private and only expose a getter that protects the field from external modifications. In addition, the fields are also read-only, which prevents internal modifications as well. This is a practice that we heavily recommend.

Another common name for such an immutable class is a data class.