I'm looking answer of this question but I can't find it. So, I have this interface:
export interface productsListFromOrder{
product_name: string,
amount: number,
price: number,
}
I made the array of this objects and I push that:
products:productsListFromOrder[] = []; //productsListFormOrder is my interface
myFunction(): void{
//...
this.products.push({
product_name: product?.name!,
price: product?.price!,
amount: element.amount ? parseInt(element.amount) : 1
})
}
Problem is that I have an error from title:
Type 'boolean' is not assignable to type 'number' I'm sure that type is correct becouse i test a few things:
- I display type of price and it display number
console.log(typeof product?.price!);
- I set type of products array on any[] and it works correctly but I want to use my interace
products:productsListFromOrder[] = [];
- I solve this problem in this way
regular_price: typeof product?.price! == 'boolean' ? 0 : product?.price!,
Ok, if I use method 2 or 3, my code works but it doesn't my goal. I need to use my interace (instead of any[] like in point 2) and code from point 3 is ugly. And what is the most important - why I have this error? Product.price has a type number. How can I write this code without typeof checking?