I'm a selftaught (web) developer. On sunny days, you can find me hiking through the Teutoburg Forest, on rainy days coding or with a good fiction novel in hand.
Thanks for your point! However I don't agree that using classes can mitigate the problem.
In your example the TS error is not caused by classes. The thing is Pizza and Beer are not compatible due to isFrozen property. Once we make classes compatible the same issue with the discount price occurs. Here is TypeScript Sandbox with class-based example.
I'm a selftaught (web) developer. On sunny days, you can find me hiking through the Teutoburg Forest, on rainy days coding or with a good fiction novel in hand.
Well observed. You still need to put some elbow greese into properly distinguishing the two from one another.
I took your example and extended it a bit using polymorphism as I missed to show it in my sloppy example above. Now even though the two classes are compatible, you will still receive the proper price for each product.
This is interesting indeed. Even according to my VSCode Tooltip, Pizza is indeed still typed as Pizza and not as Beer.
You could mitigate this problem by using classes:
Thanks for your point! However I don't agree that using classes can mitigate the problem.
In your example the TS error is not caused by classes. The thing is
PizzaandBeerare not compatible due toisFrozenproperty. Once we make classes compatible the same issue with the discount price occurs. Here is TypeScript Sandbox with class-based example.Well observed. You still need to put some elbow greese into properly distinguishing the two from one another.
I took your example and extended it a bit using polymorphism as I missed to show it in my sloppy example above. Now even though the two classes are compatible, you will still receive the proper price for each product.
Sandbox