I have an overview of houses, each house has an image which is shown inside of a cart. Since the houses are async, I want to show empty cards as long as they are not fetched. To do so I added empty objects to the array to imply houses. This works fine as long as I don't add typing to houses. After adding Typescript hates me because the object obviously don't contain the correct properties.
I know removing typing would be a solution, but I guess not a good practice. Making all properties optional isn't a good practice either, I guess. Do I have to create mocks having these properties (which would be a lot) or is there a proper way to do that?
houses: House[] = [{}, {}, {}, {}, {}];
ngOnInit(): void {
this.housesService.getGreatHouses().subscribe(houses => {
if (houses && houses.length > 0) {
this.houses = houses;
}
});
}
houses: House[] = Array(5).fill(new House())houses[0] === houses[1]will betrue. In.fillthe expression is calculated first, and then the array is filled with returned value of the expression.