One interesting thing I have noticed is the Product interface:
export interface Product{
code: string;
description: string;
type: string;
}
There is a service with a method that calls the product endpoint:
public getProducts(): Observable<Product> {
return this.http.get<Product>(`api/products/v1/`);
}
Then there is a component where the service is used to fetch the Products.
export class ShopComponent implements OnInit {
public productsArray: Product[];
ngOnInit() {
this.productService.getProducts().subscribe(res => {
this.productsArray = res;
});
}
}
However, an error occurs with this setup:
[ts] Type 'Product' is missing the following properties from type 'Product[]': length, pop, push, concat, and 26 more. [2740]
If the typing on productsArray
variable is removed, the error goes away. But it's puzzling why this doesn't work, considering the server response is an array of objects of type Products
.