Making Your Own Types
Of course, aside from using the types that are already available in JavaScript, we can define our own types. We have several options for that. We can use the JavaScript class
specification to declare our own classes, with properties and methods. A simple class can be defined with the following:
class Person { constructor(public firstName: string, public lastName: string, public age?: number) { } getFullName() { return `${this.firstName} ${this.lastName}`; } }
We can create objects of this class and use methods on them:
const person = new Person("Ada", "Lovelace"); console.log(person.getFullName());
Another way to formalize our complex structures is to use an interface:
interface Person { firstName: string; lastName: string; &...