I am fairly new to Angular/TS and I may not have worded this correctly, but I will do my best to explain.
I have defined 2 interfaces where one extends the other as shown below:
export interface CustomerModel {
firstName: string;
lastName: string;
//other things
}
export interface OrderModel extends Pick<CustomerModel, 'firstName' | 'lastName'> {
orderNumber: number;
}
I have created a service with a hardcoded instance of Customer (currently working locally on my machine and using hardcoded data. I plan to integrate a database later, but need it to function in this manner initially)
export class CustomerService {
customers: CustomerModel[];
constructor() {
this.customers = [{
firstName: 'John',
lastName: 'Doe',
//more here
},
{
firstName: 'Jane',
lastName: 'Smith',
//more here
},
]
}
getCustomers(): Observable<CustomerModel[]> {
const customers = of(this.customers);
return customers;
}
}
Now I am attempting to create an Order Service utilizing the OrderModel
export class OrderService {
orders: OrderModel[];
constructor() {
this.orders = [{
orderNumber: 123;
firstName: ???, //what should I use here to extract the name from CustomerModel
lastName: ???,
},
{
//more here
},
]
}
getOrders(): Observable<OrderModel[]> {
const orders = of(this.orders);
return orders;
}
}
When examining the value of firstName in my OrderService, it indicates that it is derived from CustomerModel, confirming that the extension was successful.
If I omit firstName and lastName from the orders array, I receive an error stating that OrderModel expects them.
Perhaps this approach is not feasible and there might be a better solution, but I have been stuck trying to make this work.
Could someone assist me in retrieving the values of the properties firstName/lastName from CustomerModel?