How can I define a type in a function argument that corresponds to one of the object properties with the same type?
For instance, if I have an object:
type Article = {
name: string;
quantity: number;
priceNet: number;
priceGross: number;
};
and I want to create a function that calculates the total price based on a specific price type property:
function calculateTotalPrice(article: Article, priceTypeProperty: NeededProperty) {
return article[priceTypeProperty] * article.quantity;
}
What should the definition of NeededProperty
be in order to ensure TypeScript that it matches either the priceNet
or priceGross
property within the Article object and has a data type of number
?