Hone logo
Hone
Problems

Mastering Computed Property Types in TypeScript

Computed property types in TypeScript allow you to derive the type of a property based on other properties within the same object. This is a powerful feature for creating more concise and maintainable type definitions, especially when dealing with complex objects where property relationships are crucial. This challenge will test your understanding of how to define and utilize computed property types effectively.

Problem Description

You are tasked with creating a TypeScript interface Product that represents an e-commerce product. The Product interface should have the following properties:

  • price: A number representing the product's price.
  • taxRate: A number representing the tax rate (e.g., 0.08 for 8%).
  • discount: A number representing a discount percentage (e.g., 0.10 for 10%).

In addition to these properties, the Product interface should also define a computed property type called totalPrice which calculates the final price of the product after applying tax and discount. The totalPrice should be calculated as follows:

  1. Calculate the price after discount: price * (1 - discount)
  2. Calculate the tax amount: (price * (1 - discount)) * taxRate
  3. Calculate the final price: (price * (1 - discount)) + (price * (1 - discount)) * taxRate

You must use a computed property type to define totalPrice. The type definition should not include an explicit totalPrice property in the interface. The goal is to demonstrate the use of computed property types to derive the type of totalPrice based on the other properties.

Examples

Example 1:

Input:
interface Product {
  price: number;
  taxRate: number;
  discount: number;
  // Computed property type for totalPrice
}

// Usage:
const product1: Product = {
  price: 100,
  taxRate: 0.08,
  discount: 0.10,
};

// The type of product1.totalPrice is inferred to be number

Output: number (inferred type of product1.totalPrice) Explanation: The totalPrice is calculated as (100 * (1 - 0.10)) + (100 * (1 - 0.10)) * 0.08 = 90 + 90 * 0.08 = 90 + 7.2 = 97.2. The computed property type correctly infers the type as number.

Example 2:

Input:
interface Product {
  price: number;
  taxRate: number;
  discount: number;
  readonly totalPrice: number; // This is incorrect - we want a computed property type
}

// Usage:
const product2: Product = {
  price: 50,
  taxRate: 0.05,
  discount: 0.0,
  totalPrice: 50 // This would be valid if totalPrice was a regular property
};

Output: (This example demonstrates an incorrect approach - do not include an explicit totalPrice property) Explanation: This example shows what not to do. The problem asks for a computed property type, not an explicit property.

Constraints

  • The price, taxRate, and discount properties must be of type number.
  • The totalPrice property must be derived using a computed property type.
  • The totalPrice calculation must be accurate.
  • The solution must be valid TypeScript code.

Notes

  • Remember that computed property types are purely type-level constructs. They do not generate runtime code. You are defining the type of totalPrice, not its value.
  • Focus on defining the Product interface correctly using a computed property type. You do not need to implement any functions to calculate the totalPrice at runtime. The type system should infer the type correctly.
  • Consider the order of operations when calculating the totalPrice.
  • Think about how TypeScript infers the type of totalPrice based on the types of price, taxRate, and discount.
Loading editor...
typescript