Within my Angular app, I have a Customer class and an ICustomer interface.
interface ICustomer { <-- obtained from backend
id: number;
name: string;
address: string;
// additional properties
}
class Customer { <-- widely used in the Angular app
// other fields
constructor(public data: ICustomer) {};
// other methods
refresh() {
// ... fetching customer data from the backend
}
}
I am trying to create an instance of Customer with an object like {'id': 5} and call its 'refresh()' method. However, this results in a compile-time error due to the lack of implementation for all members of ICustomer.
One approach to overcome this is by adjusting ICustomer to include '?' after each member variable, such as:
interface ICustomer {
id: number;
name?: string;
address?: string;
// more properties
}
After these modifications, {'id': 5} can technically be considered an instance of ICustomer.
Nevertheless, this solution feels somewhat unsatisfactory. Is there a more effective way to handle this issue?
Edit: While I agree with storing backend data in a 'data' field of type ICustomer, my query specifically pertains to the instantiation of the Customer class.