An object is a data type that stores a collection of values organized into key/value pairs. These may include primitive data or other objects.
In the following example, the person object stores various pieces of information, such as the key name, which contains the value "Lucas" of type string, and the address key, which holds another object.
const person = {
name: "Lucas", // primitive value of type string
surname: "Garcez",
age: 28, // primitive value of type number
address: {
// object type containing the keys "city" and "country"
city: "Melbourne",
country: "Australia",
},
};
A class serves as a blueprint for creating objects. It specifies an object's structure and behavior through its attributes and methods. Attributes outline the data structure (keys and value types), whereas methods define the actions that can be performed on those attributes.
class Person {
name: string; // attribute
surname: string; // attribute
age: number; // attribute
// constructor method (special method)
constructor(name: string, surname: string, age: number) {
this.name = name;
this.surname = surname;
this.age = age;
}
// method to obtain the full name: "Lucas Garcez"
getFullName() {
return `${this.name} ${this.surname}`;
}
}
The constructor is a special method within a class. It’s automatically invoked when a new object is created. Constructors are responsible for initializing the class attributes with values provided during object creation. In TypeScript, the constructor is defined using the constructor keyword, as you can see in the code above.
An instance refers to an object created from a class. For example, using the class Person mentioned above, you can create an object named lucas. Therefore, lucas is an instance of the class Person. To create an instance of an object in JavaScript or TypeScript, you use the keyword new, as demonstrated below:
const lucas = new Person("Lucas", "Garcez", 28);
lucas.name; // "Lucas"
lucas.getFullName(); // "Lucas Garcez"
It is important to note that you can create multiple objects (instances) from the same class. Although all these objects share the same structure (attributes and methods), they are independent and occupy separate memory spaces within the program.
For instance, when creating a new object:
const maria = new Person("Maria", "Oliveira", 19);
You now have a new instance of the Person class that doesn't interfere with the previously created lucas object. Each instance maintains its own values and behaviors, ensuring that manipulating one object doesn't affect the others.
An interface defines a contract establishing which attributes and methods a class must implement. In TypeScript, this relationship is established using the keyword implements. When a class implements an interface, it must include all the attributes and methods specified by that interface and their respective types.
In the following example, you have a banking system where a customer can have either CurrentAccount or SavingsAccount account. Both options must adhere to the bank’s general account rules defined by the BankAccount interface.
// Contract defining the attributes and methods of a bank account
interface BankAccount {
balance: number;
deposit(amount: number): void;
withdraw(amount: number): void;
}
class CurrentAccount implements BankAccount {
balance: number;
// The class can have other attributes and methods
// beyond those specified in the interface
overdraftLimit: number;
deposit(amount: number): void {
this.balance += amount;
}
withdraw(amount: number): void {
if (amount <= this.balance) {
this.balance -= amount;
}
}
}
class SavingsAccount implements BankAccount {
balance: number;
deposit(amount: number): void {
// can have different logic from CurrentAccount
// but must respect the method signature,
// i.e., parameters (amount: number) and return type (void)
}
withdraw(amount: number): void {
// ...
}
}