Let's consider a scenario where we have a Sequelize model called Contact, defined in the following way:
class Contact extends Model<Contact> {
id: number;
first_name: string;
last_name: string;
public static createContact = (options: Contact): Contact => new Contact(options);
public getName = (): string => `${this.first_name} ${this.last_name}`;
}
Within the createContact
function, we pass in an options object that should contain specific attributes like id, first_name, and last_name. While using Contact
as the type works, it is technically incorrect since it should only refer to the attributes.
One possible solution is to define a separate type specifically for these attributes. However, this would still require us to list them within the class, leading to redundancy. Is there a way to avoid this duplication and define the attributes in just one place?